android - Android 从可移动 sd 卡中删除文件(第二个外部存储)
问题描述
我已阅读此论坛中的所有主题,但对我没有任何帮助。我有文件路径的arraylist,我需要删除其中的一些。在我的代码中,我尝试使用:
File file = new File(filesPath.get(0));
file.delete();
if (file.exists()) {
try {
file.getCanonicalFile().delete();
} catch (IOException e) {
e.printStackTrace();
}
if (file.exists()) {
file.getAbsoluteFile().delete();
}
}
Log.e("MyLogs", file.exists() ? "true" : "false");
我从 MediaStore 获得的 filesPath,它看起来像“/storage/extSdCard/mmm/bensound-summer.mp3”。我可以毫无问题地阅读此路径,但无法删除它。这是我获取字符串数组的代码:
ArrayList<String> filesPath = new ArrayList<>();
Uri contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Images.Media.DATA};
try {
Cursor cursor = context.getContentResolver().query(
contentUri,
projection,
MediaStore.Audio.Media.IS_MUSIC + " != 0",
null,
null);
if (cursor != null && cursor.moveToFirst() && cursor.getCount() > 0) {
do {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
File file = new File(path);
if (file.exists())
filesPath.add(path);
} while (cursor.moveToNext());
return filesPath;
} else {
return null;
}
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
当然,我添加了清单的权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
从这个链接我拿了一些代码,但它并没有帮助我解决我的问题,这就是我在这里写我的问题的原因。
编辑:
刚刚找到重点:我无法从 sdcard 中删除文件(可移动)!!!从decive的存储中删除所有内容都没有任何问题。
编辑2:
try {
long id = -1;
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media._ID },
MediaStore.Audio.Media.DATA + "=?",
new String[] { file.getAbsolutePath() },
null);
if (cursor != null && cursor.moveToFirst() && cursor.getCount() > 0) {
id = cursor.getLong(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
cursor.close();
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, String.valueOf(id));
DocumentFile documentFile = DocumentFile.fromSingleUri(context, uri);
if (documentFile.delete()) {
Uri mediaContentUri = ContentUris.withAppendedId(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
id
);
context.getContentResolver().delete(mediaContentUri, null, null);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
我收到一个异常错误:
Failed to delete document
java.lang.UnsupportedOperationException: Unsupported call: android:deleteDocument
错误指向行documentFile.delete()
解决方案
首先检查您是否获得了读取外部存储和写入外部存储的权限
您可以通过此代码删除..
你可以只使用 File.delete()
File dir2 = new File(Environment.getExternalStorageDirectory() + "/Eraser/temp");
File dir = new File(Environment.getExternalStorageDirectory() + "/Eraser/Capture");
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
dir.delete();
if (dir2.isDirectory())
{
String[] children = dir2.list();
for (int i = 0; i < children.length; i++)
{
new File(dir2, children[i]).delete();
}
}
dir2.delete();
推荐阅读
- javascript - 如何使用 HTML 节点作为插槽?
- python - 使用无服务器框架将 Python 包部署到 AWS lambda 时出错
- bots - 电报机器人不工作出现此错误后:帐户暂停
- sql - SQL:选择2公里范围内的所有行
- javascript - 空输入验证不起作用?
- javascript - 将base64字符串发送到图像的逻辑是什么?
- python - discord.py 中 PermissionOverwrite 的一个很好的例子
- architecture - 单片应用程序水平可扩展性(重复):如何处理数据库?
- javascript - 使用JS在网格单元格上动态设置背景图像
- c# - 孩子不会更新 Xamarin SQLite 扩展 - 多对多关系