How to get access of a particular external folder on which i can perform CRUD operation in android 11 and higher

Viewed 25

What the need?

i have an app which deletes particular image (which user selects) in the background using a foreground service. Using the below code

private void deleteTheFile(String path) {
    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");
        }
    }
}

Its working fine upto android 10 (its working in android 10 as well because of the legacy code request in the manifest file) but as everyone know in android 11 whole storage system of android was changed so im unable to delete the file (who's path user itself give me). It works fine if i use the

MANAGE_EXTERNAL_STORAGE Permission

But play store rejects my update saying you should use media storage api instead.

yes i can use that one but i don't want to pop up a dialog every time image is about to get deleted (that too when the app is not in background, its through a foreground service).

someone on Reddit suggested the following enter image description here

But now how can i ask the user to give me permission of certain folder on which i can perform CRUD or deletion operation in the background without any pop dialog and delete that file using media store api?

0 Answers
Related