【问题标题】:Android delete file from removable sd card (second external storage)Android 从可移动 sd 卡中删除文件(第二个外部存储)
【发布时间】:2018-05-03 09:56:32
【问题描述】:

我已阅读此论坛中的所有主题,但没有任何内容适合我。我有文件路径的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 获得的文件路径,它看起来像“/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()

行

【问题讨论】:

    标签: android file delete-file


    【解决方案1】:

    它看起来像“/storage/extSdCard/mmm/bensound-summer.mp3”。

    然后该文件位于可移动微型 SD 卡上。

    Micro SD 卡对于现代 Android 系统上的应用是只读的。

    这就是您不能从中删除该文件的原因。

    不是你现在尝试的方式。

    【讨论】:

    • 请解释一下如何从 sdcard 中删除文件。为此,我将非常感谢您。你是对的!我已经检查过:如果我从外部存储(不是 sdcard)中选择文件,它会很好地删除,但如果我尝试从 sdcard 中执行相同操作 - 它不起作用
    • 谢谢先生!我想我会在几分钟内找到解决问题的方法
    • 我几乎决定了我的问题,但删除 DocumentFile 我得到了异常 Unsupported call: android:deleteDocument.我想我需要添加android.permission.MEDIA_CONTENT_CONTROL,但系统说权限只授予系统应用程序。那么,如何解决这个问题呢?任何建议,请
    • ???如果我没有看到您的代码,我无法帮助您。请在额外的代码块中发布您的代码。
    • 那是相当奇怪的代码。没见过这样的。您使用的 file.getAbsolutePath() 的值是多少?为什么要使用两个不同的 delete() 调用?
    【解决方案2】:

    首先检查您是否获得了读取外部存储和写入外部存储的权限,然后

    您可以通过此代码删除..

    你可以只使用 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();
    

    【讨论】:

      【解决方案3】:

      您确定在删除文件之前您的应用程序中已获得 WRITE_EXTERNAL_STORAGE 权限吗?

      由于您已经可以访问该文件,因此调用delete 方法应该不会有任何其他问题。此外,您还可以查看日志 cat 是否捕获到任何异常

      【讨论】:

      • 因为我写了清单的权限。我没有例外,只是 file.delete() 返回 false
      【解决方案4】:

      我希望您在 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();
      

      【讨论】:

      • 这仅在文件由于某种原因位于内部存储上时才有效?
      猜你喜欢
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-20
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多