Android设置文件为铃声

时间:2017-02-16 17:54:49

标签: java android exception

我想以编程方式从文件中设置铃声 我有Android 23+这个问题,在较低版本它运行良好:

File file = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES)) + "/" + sound.getPath() + ".mp3");
 InputStream ins = getContext().getResources().openRawResource(
    getContext().getResources().getIdentifier(sound.getPath(),
        "raw", getContext().getPackageName()));
 if (!file.exists()) {
    FileOutputStream out = new FileOutputStream(file);
    IOUtils.copy(ins, out);
 }
 ContentValues values = new ContentValues();
 values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
 values.put(MediaStore.MediaColumns.TITLE, "ring");
 values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
 values.put(MediaStore.MediaColumns.SIZE, file.length());
 values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
 values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
 values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
 values.put(MediaStore.Audio.Media.IS_ALARM, true);
 values.put(MediaStore.Audio.Media.IS_MUSIC, false);
 Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
 Uri newUri = context.getContentResolver().insert(uri, values);
 RingtoneManager.setActualDefaultRingtoneUri(
    context,
    RingtoneManager.TYPE_RINGTONE,
    uri
 );
 StartActivity.toast = Toast.makeText(getContext(), String.valueOf(uri.toString()),
    Toast.LENGTH_SHORT);
 StartActivity.toast.show();  

错误:

Failed to open directly; attempting failover: java.io.FileNotFoundException: Multiple items at content://media/external/audio/media

我的代码需要更改什么?

0 个答案:

没有答案