Possible Duplicate:
How to clear Mediastore before setting ringtone
I have a set of about 130 mp3's in my android app of different sound clips. They're listed in a listview, and when the user long-presses on one of them, it gives them the option to set it as their default ringtone or notification. For the most part, I got this working for ringtone, but its kind of inconsistent.
For example, it may set the default ringtone the first time, but the next time I try to set another clip as default ringtone, and then go into my ringtone lists, it has 'Silent' selected. Also, I've noticed that throughout my testing, the app has created 3-4 options in my ringtone list that has no corresponding file, and I have no idea how to remove these.
I'm not a very experienced android dev, so, I can't quite figure out what I'm doing wrong here. Here is the code for my setRingtone() with resourceid being passed:
public void playSound(int input){
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(input);
int size=0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
String path="/sdcard/sounds/";
String filename="my_ringtone"+".mp3";
boolean exists = (new File(path)).exists();
if (!exists){new File(path).mkdirs();}
FileOutputStream save;
try {
save = new FileOutputStream(path+filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
File k = new File(path, filename);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "MyRingtone");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
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);
//Insert it into the database
Uri newUri= this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
RingtoneManager.setActualDefaultRingtoneUri(
this,
RingtoneManager.TYPE_RINGTONE,
newUri
);
}