我从原始文件中添加铃声,如下面的代码,它可以工作。添加后,我可以打开 Android 系统铃声列表,它会出现在那里,所以我(或任何其他应用程序)可以使用它。问题是,如果我重新启动手机,那么列表中的那个条目就消失了。那么,有没有办法永久添加铃声呢?谢谢
我使用此代码添加:
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);
}