Android 从可移动 SD 卡(第二个外部存储)中删除文件
Android delete file from removable sd card (second external storage)
我已经阅读了该论坛中的所有主题,但没有任何内容适合我。我有文件路径的数组列表,我需要删除其中的一些。在我的代码中,我尝试使用:
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");
filesPath 我从 MediaStore 获得,它看起来像“/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" />
来自 this link 我为自己获取了一些代码,但它并没有帮助我解决我的问题,这就是为什么我在这里写下我的问题。
编辑:
刚刚找到重点:我无法从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();
您确定在删除文件之前在您的应用程序中获得了权限WRITE_EXTERNAL_STORAGE吗?
由于您已经可以访问该文件,因此调用 delete
方法应该没有任何其他问题。此外,您还可以查看log cat是否捕获了任何异常
我希望您在 android 清单文件中添加以下权限..
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
用于删除代码下方使用的文件..
File file = new File(selectedFilePath);
boolean deleted = file.delete();
it looks like "/storage/extSdCard/mmm/bensound-summer.mp3".
然后该文件位于可移动微型 SD 卡上。
Micro SD 卡对于现代 Android 系统上的应用程序是只读的。
这就是您不能从中删除该文件的原因。
嗯,不是你现在尝试的方式。
我已经阅读了该论坛中的所有主题,但没有任何内容适合我。我有文件路径的数组列表,我需要删除其中的一些。在我的代码中,我尝试使用:
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");
filesPath 我从 MediaStore 获得,它看起来像“/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" />
来自 this link 我为自己获取了一些代码,但它并没有帮助我解决我的问题,这就是为什么我在这里写下我的问题。
编辑:
刚刚找到重点:我无法从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();
您确定在删除文件之前在您的应用程序中获得了权限WRITE_EXTERNAL_STORAGE吗?
由于您已经可以访问该文件,因此调用 delete
方法应该没有任何其他问题。此外,您还可以查看log cat是否捕获了任何异常
我希望您在 android 清单文件中添加以下权限..
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
用于删除代码下方使用的文件..
File file = new File(selectedFilePath);
boolean deleted = file.delete();
it looks like "/storage/extSdCard/mmm/bensound-summer.mp3".
然后该文件位于可移动微型 SD 卡上。
Micro SD 卡对于现代 Android 系统上的应用程序是只读的。
这就是您不能从中删除该文件的原因。
嗯,不是你现在尝试的方式。