Setting Ringtone notification from SD card file

泄露秘密 提交于 2019-11-28 00:33:32

Correct your code to this:

if(path != null){

File k = new File(path, "moment.mp3");

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
values.put(MediaStore.Audio.Media.ARTIST, "Some Artist");
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
  MainActivity.this,
  RingtoneManager.TYPE_NOTIFICATION,
  newUri
);

Remember to be careful about testing this code! It only truthfully works the first time. If you try running the same code again, you'll be trying to shove a duplicate entry in MediaStore's table, and the SQLite database won't allow you, giving you that exact same error. You have to either rename your file and add another instance of it, or go in, remove the entry, and then try again. To do this, you could use RingDroid. Just make sure you have all audio visible, and search for your filename, then delete it from there. Alternately, you could look at their source for deleting from the table.

Weston you are right after deleting entry form database and the adding it will work out for this problem

Code for deleting entry

getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);

Uri newUri = getContentResolver().insert(uri, values);
Sam

Remember to put the "uses-permission" tag in the AndroidManifest.xml with the android:name attribute set to android.permission.WRITE_SETTINGS.

uses-permission android:name="android.permission.WRITE_SETTINGS" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!