How to delete a file from external storage in android 5

I used

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

and I used permissions WRITE_EXTERNAL_STORAGEand READ_EXTERNAL_STORAGE, but it did not work in Android 5.

Logcat message:

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

Manifest file:

<?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 DO NOT DELETE AND WRITE FILES FROM MICRO SD-CARD SINCE VERSION 4.4. It is being read only now.

+4
source share
4 answers
storage/external_SD/mm.txt

This is not a valid path. You have to use

/storage/external_SD/mm.txt

This is not external storage. This is a removable storage.

From Android 4.4, applications can no longer be written to a micro SD card.

Google decided as such.

We must live with it. But difficult.

+1
source

, , - - :

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

:

  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();

, , logcat ,

0

java.io.FileNotFoundException, , storage/external_SD/mm.txt .

, - , , .

  • /external_sd/ - storage
  • /storage/sdcard0 - SD-, Sony
  • /storage/sdcard1 - SD-,

You can also manually view your file system in Android Studio. Click "Tools" → "Android" → "Android Device Monitor". Select your device on the left and the "File explorer" tab on the right, and you can view all the files on your device.

0
source

Source: https://habr.com/ru/post/1648204/


All Articles