【问题标题】:Can't delete File with the File class无法使用 File 类删除 File
【发布时间】:2016-01-15 13:24:12
【问题描述】:

我试图通过我的应用程序删除音乐文件,但无法实现。我已经检查过

boolean exists = temp.exists();
boolean isFile = temp.isFile();

如果是真的,是的。这些方法使我返回 true。 但是当我谈到删除方法时:

boolean deleted = temp.delete();

它返回 False 并且文件没有被删除。没有异常抛出只是对我删除的变量的错误返回。

我也在使用这些权限:

<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACTION_HEADSET_PLUG"/>

有人有解决方案的想法吗? (或者我可以使用的其他类?)

编辑: 这是我的完整代码

File temp = new File(str_path);

boolean exists = temp.exists();
boolean isFile = temp.isFile();

if (exists)) {
    boolean deleted = temp.delete();
    if (deleted) {
        Toast.makeText(context, "Successful deleted " + Title_Artist, Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "Not able to delete file " + Title_Artist, Toast.LENGTH_SHORT).show();
            }
        }

(我在调试时检查了对象是否有他的路径并且它有它)

【问题讨论】:

  • temp 到底指的是什么?路径是什么?你是如何创建File 对象的?
  • 请发布更多代码 -
  • 我更新了我的帖子。您现在可以在 Edit: 部分中看到我的删除过程的整个代码
  • 这个临时工是谁?你在哪里创建的?
  • 请看我的编辑丹尼尔

标签: android file class


【解决方案1】:

删除文件音乐你必须做两个任务:

  1. 删除存储中的文件。

    public static boolean delete(File path) {
        boolean result = true;
        if (path.exists()) {
            if (path.isDirectory()) {
                for (File child : path.listFiles()) {
                    result &= delete(child);
                }
                result &= path.delete(); // Delete empty directory.
            }
            if (path.isFile()) {
            result &= path.delete();
            }
            if (!result) {
                Log.e("Delete", "Delete failed;");
            }
            return result;
        } else {
            Log.e("Delete", "File does not exist.");
            return false;
        }
    }
    
  2. 从 MediaStore 中删除文件:

    public static void deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) {
        int sdk = android.os.Build.VERSION.SDK_INT;
        if (sdk >= android.os.Build.VERSION_CODES.HONEYCOMB) {
           String canonicalPath;
           try {
               canonicalPath = file.getCanonicalPath();
           } catch (IOException e) {
               canonicalPath = file.getAbsolutePath();
           }
           final Uri uri = MediaStore.Files.getContentUri("external");
           final int result = contentResolver.delete(uri,
                    MediaStore.Files.FileColumns.DATA + "=?", new String[]{canonicalPath});
           if (result == 0) {
               final String absolutePath = file.getAbsolutePath();
               if (!absolutePath.equals(canonicalPath)) {
                contentResolver.delete(uri,
                        MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
                }
            }
        }
    }
    

您可以重置/重新扫描 MediaStore,而不是执行上面的一些代码。

注意:如果您从 SD 卡和 android 4.4 + 中删除

Android 4.4+的变化:应用不允许写(删除,修改 ...) 到外部存储,但它们的包特定目录除外。

【讨论】:

  • 但我不明白的问题是其他应用程序是如何做到的?我试过了,我无法从 extern 中删除特定文件,但应用程序 Shuffler 可以删除它。
【解决方案2】:

您评论中的路径看起来像文件位于可移动 SD 卡上。您需要 Android 4.4+ 的特殊权限才能管理或删除 SD 卡上的文件。您需要使用DocumentFile#delete()

有关使用 DocumentFile 访问可移动 SD 卡上的文件的帮助,请参阅以下 StackOverflow 帖子:

How to use the new SD card access API presented for Android 5.0 (Lollipop)?


还有一个不使用DocumentFile 的hack 可能会起作用,正如FX 文件管理器的开发人员在此解释的那样:http://forum.xda-developers.com/showpost.php?p=52151865

【讨论】:

  • hack 中的“ContentUriUtil”是什么?
【解决方案3】:

由于您正在检查该文件是否存在,因此您不能删除该文件的原因只有一个:您没有执行此操作的权限。

一个应用不能删除系统文件,也不能删除其他应用的文件。

【讨论】:

  • 但是其他音乐播放器应用程序是如何做到的呢?我需要在清单中使用的所有权限对吗?
  • 您能举个例子说明您要删除的内容吗?只是几个文件的路径。
  • /storage/extSdCard/Musik/neverlie_look.mp3
  • 那里不能编辑文件,见这个网站:support.powerampapp.com/knowledgebase/articles/…
  • 我刚刚用应用程序 Shuffle 试了一下(这有点像 poweramp),它确实有效吗?所以他们可以删除它
【解决方案4】:

假设你的文件路径是

Environment.getExternalStorageDirectory().getPath()
                        + "/Music"
                        + "/"
                        + "song.mp3"

这样删除

File dir = new File(Environment.getExternalStorageDirectory()
                        .getPath()
                        + "/Music");

if (dir.isDirectory()) {new File(dir, song.mp3).delete();}

如果您想删除音乐文件夹中的所有文件,请执行此操作

if (dir.isDirectory()) {
                    String[] children = dir.list();
                    for (int i = 0; i < children.length; i++) {
                        new File(dir, children[i]).delete();
                    }
                }

【讨论】:

    猜你喜欢
    • 2018-10-16
    • 2012-05-11
    • 1970-01-01
    • 2014-08-28
    • 2010-12-20
    • 2012-01-22
    • 2012-10-02
    • 2011-03-04
    • 2020-01-21
    相关资源
    最近更新 更多