How to delete files in android 10 and above

Viewed 104

I have to delete a particular file (screenshot image) when a certain event happens and for that I used the below permission

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

But now its only working till android 9 because of the following

WRITE_EXTERNAL_STORAGE no longer provides write access when targeting Android 11+, even when using requestLegacyExternalStorage

It says even when using requestLegacyExternalStorage.

Then what is the alternative for this problem?

And this is my code btw

File fdelete = new File(path);
        if (fdelete.exists()) {
            if (fdelete.delete()) {
                getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(path))));
                Log.i(TAG, "deleteTheFile: file deleted");
            } else {
                Log.i(TAG, "deleteTheFile: file not dellleeettteeeddd");
            }
        }
0 Answers
Related