使用从 MediaStore.Audio.Media.DATA 中提取的 uri 删除文件
Delete file using uri extracted from MediaStore.Audio.Media.DATA
在将此问题标记为重复之前,我想说我已经访问过许多类似的问题,但其中 none 已经解决了我的 problem.So,我已经使用 MediaStore 从内部存储中提取了所有音频文件,并且以字符串形式存储其 Uri,但后来当我尝试使用 file.delete 使用该 Uri 删除音频时,前者返回 false。
以下是提取歌曲 uri 及其正常工作的代码:
Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection=MediaStore.Audio.Media.IS_MUSIC+"!=0";
Cursor c=getContentResolver().query(uri,null,selection,null,null);
if(c!=null)
{
c.moveToFirst();
do {
String audioName=c.getString(c.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String artist=c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String album=c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String url=c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
String size=c.getString(c.getColumnIndex(MediaStore.Audio.Media.SIZE));
audioInfoArrayList.add(new AudioInfo(url,audioName,artist,album));
}while (c.moveToNext());
c.close();
在这里,我将所有检索到的关于音频的信息存储在自定义类型的 arrayList 中,该自定义类型还以字符串形式存储上面提取的 uri,可以使用 getUrl() 方法提取。
以下是我如何尝试删除它的代码:
Uri uri= Uri.parse(audioInfo.get(position).getUrl()); //its actually returning the url stored above in string form
deleteFile(uri.getPath());
Log.i("logText","uri.getPath() : "+uri.getPath());
Log.i("logText","audioInfo.get(position).getUrl() : "+audioInfo.get(position).getUrl());
删除文件函数:
public void deleteFile(String filePath){
if(filePath.startsWith("content://")){
ContentResolver contentResolver = context.getContentResolver();
contentResolver.delete(Uri.parse(filePath), null, null);
}else {
File file = new File(filePath);
if(file.exists()) {
if (file.delete()) {
Log.e("logText", "File deleted."); //condition 0
}else {
Log.e("logText", "Failed to delete file!"); //condition 1
}
}else {
Log.e("logText", "File not exist!"); //condition 2
}
}
}
最后是我的 logcat 示例音频文件:
01-11 13:52:34.257 19982-19982/com.example.hp.myplayer E/logText: Failed to delete file!
01-11 13:52:34.257 19982-19982/com.example.hp.myplayer I/logText: uri.getPath() : /storage/emulated/0/bluetooth/Copy Copy Fireflies-Owl_City[www.Mp3MaD.Com].mp3
01-11 13:52:34.257 19982-19982/com.example.hp.myplayer I/logText: audioInfo.get(position).getUrl() : /storage/emulated/0/bluetooth/Copy Copy Fireflies-Owl_City[www.Mp3MaD.Com].mp3
注意: 在调用 deleteFile() 条件 1 时执行的不是 2 或 0,根据我的说法,这意味着正在使用存储中的 uri 定位文件,但不能被删除并且 file.delete() 返回 false。
Android6及以上Android在清单中请求读写外部存储权限是不够的。
您应该添加代码来要求您的应用的用户确认请求的权限。
Google 运行时权限。
在将此问题标记为重复之前,我想说我已经访问过许多类似的问题,但其中 none 已经解决了我的 problem.So,我已经使用 MediaStore 从内部存储中提取了所有音频文件,并且以字符串形式存储其 Uri,但后来当我尝试使用 file.delete 使用该 Uri 删除音频时,前者返回 false。 以下是提取歌曲 uri 及其正常工作的代码:
Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection=MediaStore.Audio.Media.IS_MUSIC+"!=0";
Cursor c=getContentResolver().query(uri,null,selection,null,null);
if(c!=null)
{
c.moveToFirst();
do {
String audioName=c.getString(c.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String artist=c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String album=c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String url=c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
String size=c.getString(c.getColumnIndex(MediaStore.Audio.Media.SIZE));
audioInfoArrayList.add(new AudioInfo(url,audioName,artist,album));
}while (c.moveToNext());
c.close();
在这里,我将所有检索到的关于音频的信息存储在自定义类型的 arrayList 中,该自定义类型还以字符串形式存储上面提取的 uri,可以使用 getUrl() 方法提取。 以下是我如何尝试删除它的代码:
Uri uri= Uri.parse(audioInfo.get(position).getUrl()); //its actually returning the url stored above in string form
deleteFile(uri.getPath());
Log.i("logText","uri.getPath() : "+uri.getPath());
Log.i("logText","audioInfo.get(position).getUrl() : "+audioInfo.get(position).getUrl());
删除文件函数:
public void deleteFile(String filePath){
if(filePath.startsWith("content://")){
ContentResolver contentResolver = context.getContentResolver();
contentResolver.delete(Uri.parse(filePath), null, null);
}else {
File file = new File(filePath);
if(file.exists()) {
if (file.delete()) {
Log.e("logText", "File deleted."); //condition 0
}else {
Log.e("logText", "Failed to delete file!"); //condition 1
}
}else {
Log.e("logText", "File not exist!"); //condition 2
}
}
}
最后是我的 logcat 示例音频文件:
01-11 13:52:34.257 19982-19982/com.example.hp.myplayer E/logText: Failed to delete file! 01-11 13:52:34.257 19982-19982/com.example.hp.myplayer I/logText: uri.getPath() : /storage/emulated/0/bluetooth/Copy Copy Fireflies-Owl_City[www.Mp3MaD.Com].mp3 01-11 13:52:34.257 19982-19982/com.example.hp.myplayer I/logText: audioInfo.get(position).getUrl() : /storage/emulated/0/bluetooth/Copy Copy Fireflies-Owl_City[www.Mp3MaD.Com].mp3
注意: 在调用 deleteFile() 条件 1 时执行的不是 2 或 0,根据我的说法,这意味着正在使用存储中的 uri 定位文件,但不能被删除并且 file.delete() 返回 false。
Android6及以上Android在清单中请求读写外部存储权限是不够的。
您应该添加代码来要求您的应用的用户确认请求的权限。
Google 运行时权限。