6

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());
1
  • 1
    where is mCr initialized? and post the stacktrace Mar 15, 2014 at 5:24

2 Answers 2

14

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);
2
  • 3
    This solution will work, but not the recommended way to do it. You should query for existence of the uri, and insert only if not present instead of invoking a delete-insert pair each time Nov 18, 2015 at 17:15
  • 1
    Is anybody else getting the following exception using this code on Android Pie? android.database.sqlite.SQLiteException: table files has no column named title_resource_uri (code 1 SQLITE_ERROR): , while compiling: INSERT INTO files(_data,title_key,parent,_display_name,format,title_resource_uri,is_music,title,is_ringtone,artist_id,bucket_id,_size,is_alarm,date_added,date_modified,mime_type,media_type,is_notification,album_id,bucket_display_name) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
    – edy
    Feb 19, 2019 at 15:41
2

Don't set the duplicate file name

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 26, 2021 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.