可能重复:
How to clear Mediastore before setting ringtone
在我的android应用程序中,我有一套大约130 mp3的不同声音剪辑。它们在listview中列出,当用户长时间按压其中一个时,它给了他们将其设置为默认铃声或通知的选项。在大多数情况下,我的工作是铃声,但这是不一致的。
例如,它可能第一次设置默认铃声,但下一次尝试将另一个剪辑设置为默认铃声,然后进入我的铃声列表,它选择了“沉默”。另外,我注意到在我的测试过程中,应用程序在我的铃声列表中创建了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
);
}发布于 2012-02-19 22:45:50
很可能,getBaseContext是您的问题。基本上下文将随着其他事情的发生而改变。你需要你的应用程序的上下文。
https://stackoverflow.com/questions/9353799
复制相似问题