我正在尝试构建一个Andorid应用程序,将.mp3文件设置为铃声,通知或警报。在搜索谷歌后,我遇到了一个适用于一群不同的人的例子。运行应用程序时,我收到一条错误消息,指出应用程序已停止工作,LogCat
将此消息吐出
java.lang.UnsupportedOperationException: Unknown URI: content://media//storage/sdcard/media/audio/ringtones/joytotheworld_symphony.mp3/audio/media"
我做了一些调试,我注意到错误来自
Uri uri = MediaStore.Audio.Media.getContentUri(k.getAbsolutePath());
以下是设置铃声/通知/闹钟的对象的其余代码:
public boolean saveas(int type) {
byte[] buffer = null;
String filename = null;
InputStream fIn = getBaseContext().getResources().openRawResource(
R.raw.joytotheworld_symphony);
int size = 0;
String collected = null;
try {
size = fIn.available();
buffer = new byte[size];
while(fIn.read(buffer) != -1){
collected = new String(buffer);
}
} catch (IOException e) {
return false;
}finally{
try {
fIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String path = Environment.getExternalStorageDirectory().getPath()
+ "/media/audio/ringtones/";
filename = "joytotheworld_symphony" + ".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.close();
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
}
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, filename);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
// This method allows to change Notification and Alarm tone also. Just
// pass corresponding type as parameter
if (RingtoneManager.TYPE_RINGTONE == type) {
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
} else if (RingtoneManager.TYPE_NOTIFICATION == type) {
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
} else if (RingtoneManager.TYPE_ALARM == type) {
values.put(MediaStore.Audio.Media.IS_ALARM, true);
}
Uri uri = MediaStore.Audio.Media.getContentUri(k.getAbsolutePath());
Uri newUri = Set.this.getContentResolver().insert(uri, values);
try{
RingtoneManager.setActualDefaultRingtoneUri(Set.this, type, newUri);
}catch (Throwable t){
Log.d("mn", "catch exception");
}
// Insert it into the database
this.getContentResolver()
.insert(MediaStore.Audio.Media.getContentUriForPath(k
.getAbsolutePath()), values);
return true;
}
我不知道如何解决这个问题,我尝试在线搜索,但我找不到修复(可能是因为我没有在正确的地方搜索)。
是否有人可以帮助我。
提前致谢,
的Maza。
编辑:我忘了提到我还写了清单中需要的权限,AVD确实有SD设置500 Mb的空间