可能重复:
如何在设置铃声之前清除 Mediastore
我的 android 应用程序中有一组大约 130 个 mp3 的不同声音片段。它们列在列表视图中,当用户长按其中一个时,它会为他们提供将其设置为默认铃声或通知的选项。在大多数情况下,我把它用于铃声,但它有点不一致。
例如,它可能第一次设置默认铃声,但下次我尝试将另一个剪辑设置为默认铃声,然后进入我的铃声列表时,它选择了“静音”。另外,我注意到在整个测试过程中,该应用程序在我的铃声列表中创建了 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
);
}