如何从 android 5 中的外部存储中删除文件

How to delete file from external storage in android 5

我用过

File f=new File(MyPath);
f.delete();

并且我使用了权限 WRITE_EXTERNAL_STORAGEREAD_EXTERNAL_STORAGE,但它在 Android 中不起作用 5.

Logcat 留言:

java.io.FileNotFoundException: storage/external_SD/mm.txt: open failed: EACCES (Permission denied)  

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mahestan_programming.myapplication">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

WE CANNOT DELETE AND WRITE FILES FROM MICRO SD-CARD SINCE VERSION 4.4. It is read only now.

试试这个:

  private ArrayList<String> _filePaths = new ArrayList<String>();
  final String imgPath=_filePaths.get(_postion);
        final Snackbar snackbar = Snackbar
                .make(view, "Delete Image?", Snackbar.LENGTH_LONG)
                .setAction("DELETE", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        File file = new File(imgPath);
                        file.delete();

                        Snackbar snackbar1 = Snackbar.make(view, "Image Deleted!", Snackbar.LENGTH_SHORT);
                        notifyDataSetChanged();
                        View sbView = snackbar1.getView();
                        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                        textView.setTextColor(Color.YELLOW);
                        snackbar1.show();

                    }
                });
        snackbar.setActionTextColor(Color.RED);
        View sbView = snackbar.getView();
        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.YELLOW);
        snackbar.show();

如果它不起作用意味着 post logcat 和您正在使用的代码

请仔细检查您的文件路径,路径如下所示:

String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Audio/audio_20160622_081844.3gp";
File file = new File(filePath);
if (file.exists()) {
   file.delete();
}

您收到 java.io.FileNotFoundException,这意味着文件路径 storage/external_SD/mm.txt 是错误的。

在我的设备上有不同的位置,我想其中之一就是您想要的位置。

  1. /external_sd/ - 没有 storage 前缀
  2. /storage/sdcard0 - 索尼提供的虚拟SD卡
  3. /storage/sdcard1 - 真正的物理 SD 卡,插入设备

您还可以在 Android Studio 中手动查看您的文件系统。请点击"Tools" -> "Android" -> "Android Device Monitor"。在左侧选择您的设备,然后在右侧选择 "File explorer" 选项卡,您将能够查看设备上的所有文件。

storage/external_SD/mm.txt

该路径无效。你应该使用

/storage/external_SD/mm.txt

这本来就不是外部存储。它是移动存储。

从Android 4.4 开始,应用程序无法再写入 micro SD 卡。

Google 就这样决定了。

我们不得不忍受它。但是很难。