0

私の最初の(着信音)アプリの最後の部分で、チュートリアルや友人の助けを借りて、 RAWフォルダからmp3をAndroidの着信音、通知、およびアラーム音として使用するコード。私はエミュレータでテストしましたが、うまくいきましたが、S6edgeで何もテストしても何も起こりませんでした。誰かがこのコードで何が問題になるか、誰かが私により良いコーディングソリューションを教えてくれるか教えてください。コード:RAWフォルダからmp3をAndroidの着信音、通知、アラーム音として設定する

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

関連する問題