【发布时间】:2012-04-23 18:06:21
【问题描述】:
我有两个问题:
当我在 android 4.0.3 设备上拨打我的
saveas(ressound);(灵感来自于stealthcopter.com)时,铃声被很好地激活为铃声,但在 2.3.3 设备上,所选铃声是静音的。我的代码哪里出错了?我想知道的第二件事是如何删除文件。更重要的是,如何将它从媒体存储中取出,如果我将其从 sd 中删除,它会自动发生吗?
更新:
添加了删除整个文件夹和内容的代码,效果很好而且很干净! (见下文)
当我按下按钮时尝试调用此代码:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
但它不会清除媒体存储,它需要重新启动才能工作。 我还尝试了开发工具中的媒体扫描,但这不起作用。他们提到一些应用程序需要重启,但是如何在不重启整个设备的情况下重启负责显示可用铃声列表的服务?
变量:
String soundname;
int ressound;
int savetype;
String path="/sdcard/mysounds/";
savetype = RingtoneManager.TYPE_RINGTONE;
ressound = R.raw.sound1;
soundname = "sound1";
保存(回复);
//save as
public boolean saveas(int ressound){
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
int size=0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
return false;
}
String filename= soundname +".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) {
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, soundname);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Elvis");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
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);
// set as ringtone
RingtoneManager.setActualDefaultRingtoneUri(this, savetype, newUri);
return true;
}
删除();
public void delete() {
File a = new File(path, "sound1.mp3");
a.delete();
File b = new File(path, "sound2.mp3");
b.delete();
}
删除文件(字符串路径);
public static void deleteFiles(String path) {
File file = new File(path);
if (file.exists()) {
String deleteCmd = "rm -r " + path;
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(deleteCmd);
} catch (IOException e) { }
}
}
【问题讨论】:
-
目前市面上比较多的app有这个问题,不知道能不能解决???
-
可能是因为它会覆盖,如果文件已经存在的话。不知道怎么解决....
-
我做了一个删除功能,整个地图和内容都被删除了。因此,我检查了上述假设,没有运气。
-
或者答案应该在uri的方向吗?需要研究一下......制作一个好的和坚固(和可逆)的音板比我想象的要难
标签: android delete-file mediastore ringtone