Search code examples
androidfileringtone

Android set ringtone works only first time


i am having a small problem with my android application. I have some mp3 files in my app which i want to be able to set as default ringtone or notification. I followed the instructions on this post Setting Ringtone in Android and it kind of works.

here is my code:

Creating the file to set as ringtone:

public static String createFile(String raw, int choice){
    File newSoundFile = null;
    String path = (choice == 0) ? "/mnt/sdcard/media/mySoundsRingtone.mp3" : "/mnt/sdcard/media/mySoundsNotification.mp3";
    newSoundFile = new File(path);
    if(newSoundFile.exists()){
        try {
            newSoundFile.delete();
        } catch (Exception e) {
            Toast.makeText(cont, "delete failed", Toast.LENGTH_SHORT).show();
            return null;
        }

    }

    try {
        newSoundFile.createNewFile();
    } catch (IOException e) {
        Toast.makeText(cont, "file creation failed", Toast.LENGTH_SHORT).show();
        return null;
    }
    //Toast.makeText(cont, "file created", Toast.LENGTH_SHORT).show();



    Uri mUri = Uri.parse("android.resource://my.package.name/"+raw);

    ContentResolver mCr = cont.getContentResolver();
    AssetFileDescriptor soundFile;
    try {
           soundFile= mCr.openAssetFileDescriptor(mUri, "r");
       } catch (FileNotFoundException e) {
           Toast.makeText(cont, "AssetFileDescriptor failed", Toast.LENGTH_SHORT).show();
           soundFile=null; 
           return null;
       }

       try {
          byte[] readData = new byte[1024];
          FileInputStream fis = soundFile.createInputStream();
          FileOutputStream fos = new FileOutputStream(newSoundFile);
          int i = fis.read(readData);

          while (i != -1) {
            fos.write(readData, 0, i);
            i = fis.read(readData);
          }

          fos.close();
       } catch (Exception io) {
           Toast.makeText(cont, "copy failed", Toast.LENGTH_SHORT).show();
           return null;
       }

    return path;
}

Setting the file as ringtone:

public static void setValues(String path, int choice){
    File newSoundFile = new File(path);

    ContentValues values = new ContentValues();
       values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
       values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
       values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
       values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
       if(choice == 0){
           values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
           values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
       }
       else{
           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);

       Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
       Uri newUri = mCr.insert(uri, values);


       try {
           if(choice == 0)RingtoneManager.setActualDefaultRingtoneUri(cont, RingtoneManager.TYPE_RINGTONE, newUri);
           else RingtoneManager.setActualDefaultRingtoneUri(cont, RingtoneManager.TYPE_NOTIFICATION, newUri);
       } catch (Throwable t) {
           Log.d("LOG", "catch exception");
       }


}

Calling both:

public static void setRingTone(String raw, int choice){
    setValues(createFile(raw, choice), choice);


}

so what happens is: the first time that i select a ringtone the new file is created, and the sound is assigned properly as the ringtone. If I click again to select a ringtone the old ringtone file is deleted and recreated properly, but it in not set as a ringtone (when i get called i get only vibration). BUT if I delete the .mp3 file manually and go to my app and select ringtone again it works.. Why does it behave differently when i delete the file from my code than when it is deleted manually from a file browser?

Any ideas what might be the problem?

Thanks in advance!


Solution

  • Ok after a few hours of searching i found the answer on this post: setting audio file as Ringtone

    (i don't know how to mark my question as a duplicate)