I am trying to set a custom ringtone by pressing a button but getting null from ContentResolver's insert method. Following is the code resopnsible for setting the ringtone. I checked on stackoverflow itself and some users said its working for them but in my case I get the variable "newUri" as null
The sound file gets created at the desired location so I know that "newSoundFile" is ok. Upon printing "values" variable in log gives the following.
Values are title=my ringtone is_notification=true mime_type=audio/mp3 is_alarm=true is_ringtone=true _size=8733 is_music=false artist=None _data=/mnt/sdcard/media/ringtone/myringtone.mp3
try
{
uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
Log.d("Test", uri.toString());
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
values.put(MediaStore.Audio.Media.ARTIST, "None");
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);
Log.d("Test","Values are "+values.toString());
newUri = mCr.insert(uri, values);
}
catch (Exception ee)
{
Log.d("Test", "Exception is " + ee);
}
if(newUri == null)
Log.d("Test", "newUri is Null");
else
Log.d("Test", "newUri is "+newUri.toString());
Use this code.
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "None");
values.put(MediaStore.MediaColumns.SIZE, 215454);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile
.getAbsolutePath());
getContentResolver().delete(
uri,
MediaStore.MediaColumns.DATA + "=\""
+ newSoundFile.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
RingtonesPlaying.this, RingtoneManager.TYPE_RINGTONE,
newUri);
Don't set the duplicate file name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With