views:

166

answers:

1

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.

A: 

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