I add a ring tone from a raw file like this code below, and it works. Once I added it, I can open Android System Ringtones list and it appears there, so I (or any other app) can use it. The problem is that if I restart the phone then that entry from the list is gone. So, is there a way to permanently add a ringtone? Thanks
I use this code for adding:
private void AddRingTone() //I assume that sdcard directory exists and it is empty
{ //We first copy the raw resource to sdcard:
String sPath = Environment.getExternalStorageDirectory() + "/AnyPath"; //AnyPath already exists.
File newSoundFile = new File(sPath, "MyRingtone");
Uri mUri = Uri.parse("android.resource://" + getPackageName()+ "/" + R.raw.myringtone);
AssetFileDescriptor soundFile;
try
{ soundFile= getContentResolver().openAssetFileDescriptor(mUri, "r");
}catch (FileNotFoundException e)
{ soundFile=null;
MessageBox("Cannot open " + mUri.toString());
}
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(IOException io)
{ MessageBox("RingtoneManager:\n" + io.toString());
}
//Now we add it to the system list:
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "my ring tone");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/oog");
values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
values.put(MediaStore.Audio.Media.ARTIST, "me");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
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 = getContentResolver().insert(uri, values);
}
The reason it disappears, is its not stored in the right place!
The official spot where Android keeps tabs on the media files is located in /sdcard/media/audio/Ringtones
or /sdcard/media/audio/notifications
, whichever suits best!
Android knows zero about a directory called AnyPath
and the media scanner does not pick it up!