1

我正在制作一个视频下载器应用程序,将应用程序下载的文件保存和删除到外部存储没有问题,但是从我的计算机传输的任何文件都不能被应用程序删除。

这是一个真正的问题,因为它是我想要的关键功能之一。这是我正在使用的代码:

public boolean deleteDataFromStorage(Data toDelete) {

    //The file object soon to be deleted
    File f = null;

    Log.e(TAG, "Deleting " + toDelete.fileName);

    // Delete file from storage
    try {

        // Get file to delete
        f = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + DIRECTORY + toDelete.fileName);

    } catch (IOException e) {

        Log.e(TAG, e.toString());

        // Print to stack trace
        e.printStackTrace();
    }

    // Delete file
    if(f.delete()) {
        return true;
    } else {

        Log.e(TAG, "Failed to delete " + toDelete.fileName);
        return false;
    }
}

由于该f.delete()函数不会引发任何异常,我不知道问题出在哪里。我唯一能想到的是该应用程序无权删除在 Windows 中创建的文件,但我已经从应用程序商店下载了删除传输文件没有问题的应用程序。

任何帮助将不胜感激。

4

1 回答 1

2

As per your comment, since f.isFile() and f.exists() returns false, your f is not a file, in other words, you're getting the path wrong.

Print to the logs f.getAbsolutePath(), check what it is, and then it should be easy to fix.

于 2013-05-18T16:07:12.990 回答