无法删除SD卡上的文件

时间:2018-07-14 05:42:17

标签: java android delete-file android-sdcard

我正在尝试删除SD卡上的某些文件,但是我没办法了。

我尝试了File.delete()方法,然后我尝试了file.getCanonicalFile().delete() 还有很多 .. 我的应用程序只能删除设备存储上的文件。 我已经在清单文件中定义了所需的权限,如下所示。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

并且我也在代码中请求许可:

  if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        new AlertDialog.Builder(this)
                .setTitle(R.string.read_storage_message)
                .setPositiveButton(R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ActivityCompat.requestPermissions(activity,
                                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MainActivity.READ_STORAGE_PERMISSION_ID);
                            }
                        }
                )
                .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                AppKiller.kill();

                            }
                        }
                ).show();
    }
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        new AlertDialog.Builder(this)
                .setTitle(R.string.write_storage_message)
                .setPositiveButton(R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ActivityCompat.requestPermissions(activity,
                                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MainActivity.STORAGE_PERMISSION_ID);
                            }
                        }
                )
                .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                AppKiller.kill();

                            }
                        }
                ).show();
    }
}

我正在使用以下方法删除文件,但是正如我之前提到的,我已经使用了Stack-overflow等中的许多解决方案。

protected boolean delete(Context context) throws Exception {
        boolean delete = false;
        File file = new File(Environment.getExternalStorageDirectory().getPath() + getName());
        if (file.exists()) {
            delete = file.delete();
            if (!delete) {
                delete = file.getCanonicalFile().delete();
            }
            if (!delete) {
                String deleteCmd = "rm -r " + file.getAbsolutePath();
                Runtime runtime = Runtime.getRuntime();
                runtime.exec(deleteCmd);
            }
        }
        return delete;
    }

可能是因为我要获得READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE权限,但是在代码中我只是获得了READ_EXTERNAL_STORAGE的授权,而另一个却被Android忽略了(不会我接受第一个选项后,显示带有许可拒绝选项的弹出许可WRITE_EXTERNAL_STORAGE弹出窗口)。是不是因为它们在android中具有相同的权限级别?

怎么了?

3 个答案:

答案 0 :(得分:2)

如果要从SD卡删除文件,可以使用以下代码:

File file = new File("/sdcard/myfile.txt");
boolean deleted = file.delete(); 

答案 1 :(得分:1)

如果要从SD卡删除文件,可以使用以下代码:

File file = new File(selectedFilePath);
boolean deleted = file.delete();

其中selectedFilePath是要删除的文件的路径。

  

例如:selectedFilePath =   /sdcard/YourDirectory/TestFile.mp3

如果您使用的是> 1.6 SDK,还必须授予权限

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

在AndroidManifest.xml文件中

希望这可能对您有帮助

答案 2 :(得分:1)

是的,您无法再使用File类从现代Android版本的SD卡中删除文件。

请改用Storage Access Framework。

看看Intent.OPEN_DOCUMENT_FILEOPEN_DOCUMENT_TREE