0

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
    );
}
Community
  • 1
  • 1
fog
  • 43
  • 6
  • The following question on Stack Overflow has solved my issue: http://stackoverflow.com/questions/7302037/how-to-clear-mediastore-before-setting-ringtone – fog Feb 27 '12 at 17:48

1 Answers1

0

Most likely, getBaseContext is your problem. The base context will change as other things happen. You want the context of your app.

Community
  • 1
  • 1
stark
  • 12,615
  • 3
  • 33
  • 50
  • This kinda worked, but, I'm still getting multiple listings in the ringtone list each time i set a new default ringtone. The above code keeps over-writing the file with the same filename which is what I want, but when I go into android's ringtone selection, its listing a bunch of ringtones as their name's in my raw/ directory (sound1, sound2, sound3, etc.) instead of the "MyRingtone". Any idea how to fix that? – fog Feb 21 '12 at 18:35