以下のコードのように生のファイルから着信音を追加すると、機能します。追加したら、Android System Ringtones リストを開くことができ、そこに表示されるので、私 (または他のアプリ) はそれを使用できます。問題は、電話を再起動すると、リストからそのエントリが失われることです。では、着信音を永続的に追加する方法はありますか? ありがとう
このコードを使用して追加します。
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);
}