可能重複設置默認鈴聲:
How to clear Mediastore before setting ringtone問題與代碼
我有一組約130 MP3的,在我不同的聲音剪輯的Android應用程序。它們列在一個列表視圖中,當用戶長按其中的一個時,它可以讓他們選擇將其設置爲默認的鈴聲或通知。在大多數情況下,我得到了鈴聲的工作,但它的一種不一致。
例如,它可能會首次設置默認鈴聲,但下一次我嘗試將另一個剪輯設置爲默認鈴聲,然後進入我的鈴聲列表中時,會選擇「無聲」。另外,我注意到,在我的測試中,應用程序在我的鈴聲列表中創建了3-4個沒有相應文件的選項,我不知道如何刪除這些選項。
我不是一個非常有經驗的android開發人員,所以,我不能完全弄清楚我在這裏做錯了什麼。這裏是正在傳遞我setRingtone()與RESOURCEID代碼:
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
);
}
堆棧溢出以下問題已經解決了我的問題:http://stackoverflow.com/questions/7302037/how-to-clear-mediastore-before-setting-ringtone – fog 2012-02-27 17:48:01