2017-03-14 11 views
0

내 첫 (벨소리) 응용 프로그램의 마지막 부분과 몇 가지 자습서를 기반으로 약간의 문제가 있고 친구의 도움을 받아 안드로이드 벨소리, 알림 및 알람 소리로 RAW 폴더에서 mp3를 가지고 코드. 에뮬레이터에서 테스트 해 보았지만 작동했지만, 내 S6edge +에서 아무 일도 일어나지 않았다면 누군가이 코드에 어떤 문제가 있는지 또는 누군가가 더 나은 코딩 솔루션을 가르쳐 줄 수 있는지 말해 줄 수 있습니까?안드로이드 벨소리, 알림 및 알람 소리로 RAW 폴더에서 mp3 설정

코드 :

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    setSong(this, RingtoneManager.TYPE_RINGTONE, R.raw.test, "TestR"); 
    setSong(this, RingtoneManager.TYPE_NOTIFICATION, R.raw.test, "TestN"); 
    setSong(this, RingtoneManager.TYPE_ALARM, R.raw.test, "TestA"); 
} 


/** 
* In this method, we need to copy the mp3 file to the sd card location from 
* where android picks up ringtone files 
* After copying, we make the mp3 as current ringtone 
* 
* @param context 
* @param type 
* @param songId 
* @param ringtoneTitle 
* @return 
*/ 

public static boolean setSong(Context context, int type, int songId, String ringtoneTitle) { 
    byte[] buffer = null; 
    InputStream fIn = context.getResources().openRawResource(
      songId); 
    int size = 0; 

    try { 
     size = fIn.available(); 
     buffer = new byte[size]; 
     fIn.read(buffer); 
     fIn.close(); 
    } catch (IOException e) { 
     return false; 
    } 

    String path = Environment.getExternalStorageDirectory().getPath() 
      + "/media/audio/ringtones/"; 

    String filename = ringtoneTitle + ".mp3"; 

    boolean exists = (new File(path)).exists(); 
    if (!exists) { 
     new File(path).mkdirs(); 
    } 

    FileOutputStream save; 
    try { 
     save = new FileOutputStream(path + filename); 
     save.write(buffer); 
     save.flush(); 
     save.close(); 
    } catch (FileNotFoundException e) { 
     return false; 
    } catch (IOException e) { 
     return false; 
    } 

    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, 
      Uri.parse("file://" + path + filename))); 

    File k = new File(path, filename); 

    ContentValues values = new ContentValues(); 
    values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath()); 
    values.put(MediaStore.MediaColumns.TITLE, ringtoneTitle); 
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*"); 
    values.put(MediaStore.MediaColumns.SIZE, k.length()); 

    // This method allows to change Notification and Alarm tone also. Just 
    // pass corresponding type as parameter 
    if (RingtoneManager.TYPE_RINGTONE == type) { 
     values.put(MediaStore.Audio.Media.IS_RINGTONE, true); 
    } else if (RingtoneManager.TYPE_NOTIFICATION == type) { 
     values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); 
    } else if (RingtoneManager.TYPE_ALARM == type) { 
     values.put(MediaStore.Audio.Media.IS_ALARM, true); 
    } 

    Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()); 

    String selection = MediaStore.MediaColumns.DATA + " =?"; 
    String[] selectionArgs = {k.getAbsolutePath()}; 
    Uri newUri = null; 

    // Check if record exist 
    Cursor c = context.getContentResolver().query(uri, null, selection, selectionArgs, null); 
    if (c != null) { 
     if (c.getCount() > 0) { 
      // Record exist 
      // Update record 
      while (c.moveToNext()) { 
       int idIndex = c.getColumnIndex(MediaStore.Audio.Media._ID); 
       int dataIndex = c.getColumnIndex(MediaStore.Audio.Media.DATA); 
       String strId = c.getString(idIndex); 
       String songPath = c.getString(dataIndex); 
       newUri = MediaStore.Audio.Media.getContentUriForPath(songPath); 
       String condition = MediaStore.Audio.Media._ID + " =?"; 
       String[] selectionArg = {strId}; 
       context.getContentResolver().update(uri, values, condition, selectionArg); 
      } 

     } else { 
      // Record does not exist, create new 
      newUri = context.getContentResolver().insert(uri, values); 
     } 
    } 

    if (newUri != null) { 
     RingtoneManager.setActualDefaultRingtoneUri(context, type, 
       newUri); 
     return true; 
    } 

    return false; 
} 

} 물론

, 내가 가진 권한

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>` 

원시 폴더에 test.mp3 파일

+0

marshmallow에 대한 사용 권한 확인 – user2025187

답변

0

사용자는 Android 6.0 (API 수준 23)부터 앱을 실행하는 동안 앱을 실행하는 동안 앱에 대한 권한을 부여합니다. 앱을 설치할 때가 아닙니다. 이 방법은 사용자가 앱을 설치하거나 업데이트 할 때 권한을 부여 할 필요가 없기 때문에 앱 설치 프로세스를 간소화합니다. 또한 사용자가 앱의 기능을 더 잘 제어 할 수 있습니다. 예를 들어 사용자는 카메라 앱에 카메라에 대한 액세스 권한을 부여 할 수는 있지만 기기 위치에 대한 권한 부여는 선택할 수 없습니다. 사용자는 앱의 설정 화면으로 이동하여 언제든지 사용 권한을 취소 할 수 있습니다.

응용 프로그램에서 런타임 권한을 처리해야합니다. 자습서는 아래 링크를 참조하십시오. Runtime Permission in Android

+0

그런 경우는 아니지만 Google Play에서 앱을 다운로드하여 동일한 휴대 전화에서 실행하고 벨소리를 설정하는 동안 권한을 요청하지 않았습니다. , 그렇게해서는 안된다. – somidurlan