Setting Ringtone notification from SD card file
Asked Answered
G

3

2

My goal is to set the users notification sound from a file that is stored onto the SD card from with in the application. I am using this code:

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.SIZE, 215454);
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
    values.put(MediaStore.Audio.Media.ARTIST, "Some Artist");
    values.put(MediaStore.Audio.Media.DURATION, 230);
    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);
    values.put(MediaStore.MediaColumns.DISPLAY_NAME, "Some Name");

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

    RingtoneManager.setActualDefaultRingtoneUri(
      MainActivity.this,
      RingtoneManager.TYPE_NOTIFICATION,
      newUri
    );
    //RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, newUri);
    Toast.makeText(this, "Notification Ringtone Set", Toast.LENGTH_SHORT).show();
}

When I run this on the device I keep getting the error:

06-12 15:19:36.741: ERROR/Database(2847): Error inserting is_alarm=false is_ringtone=false artist_id=35 is_music=false album_id=-1 title=My Song title duration=230 is_notification=true title_key=%D%\%%P%H%F%8%%R%<%R%B%4% mime_type=audio/mp3 date_added=1276370376 _display_name=moment.mp3 _size=215454 _data=/mnt/sdcard/Android/data/_MY APP PATH_/files/moment.mp3
06-12 15:19:36.741: ERROR/Database(2847): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

I have seen others using this technique and I can't find any documentation on which values actually need to be passed in to successfully add the file into the Android system so that it can be set as a notification.

Galah answered 12/6, 2010 at 19:40 Comment(0)
B
14

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.

Boracite answered 5/10, 2010 at 4:52 Comment(1)
Thank you very much for clearing up the problem with doubles. Of course this happens when you try to figure out code and the message is not very clear what's the problem. So thanks again saving my sleep ;-)Nonexistence
V
12

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);
Vapory answered 6/1, 2011 at 8:50 Comment(0)
C
6

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" />
Coaly answered 9/11, 2010 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.