Setting Ringtone notification from SD card file

Posted by sgarman on Stack Overflow See other posts from Stack Overflow or by sgarman
Published on 2010-06-12T19:40:56Z Indexed on 2010/06/12 19:42 UTC
Read the original article Hit count: 332

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about android

Related posts about ringtone