无法删除SD卡上的文件

Cannot delete files on 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 g运行ted 而另一个正在得到从 Android 中忽略(在我接受第一个选项后,它不会显示带有允许-拒绝选项的权限 WRITE_EXTERNAL_STORAGE 弹出窗口)。是不是因为他们在 android 中的权限级别相同?

有什么问题吗?

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

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

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

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

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

for example: selectedFilePath = /sdcard/YourDirectory/TestFile.mp3

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

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

在AndroidManifest.xml文件中

希望对您有所帮助

是的,在现代 Android 版本上,您不能再使用文件 class.

从 SD 卡中删除文件

改用存储访问框架。

看看 Intent.OPEN_DOCUMENT_FILEOPEN_DOCUMENT_TREE