'How use File("/sdcard/myfile") without READ_EXTERNAL_STORAGE on android 11?

After my Application have been refused by the play Store I have to remove MANAGE_EXTERNAL_STORAGE to the manifest. This authorisation is only for app like antivirus or file manager.

I need to get a file like this :

File file = new File("/sdcard/Download/Myfile.pdf");

This actually produce the following error on android 11 :

FILE NOT FOUND/sdcard/Download/GE-ACTIONS-03.pdf: open failed: EACCES (Permission denied)

Are there a possibility to get The File in this way ?

I need this file to create a fileInputStream like this :

fileInputStream = new FileInputStream(file);

(I already read other answer on stack overflow but they all use MANAGE_EXTERNAL_STORAGE.)



Solution 1:[1]

well, question in title is pretty clear, same as answer: you can't. these all permissions and restrictions are created for purpose (securing user), not for breaking/ommiting them

currently on newest system versions you have to use Scoped Storage and you can freely store files onle there. afaik you can get access to Download folder (and other media ones) using Media Store API, but on devices with Scoped Storage your app will see only own files stored in this folder (downloaded by itself, not by another apps)

Solution 2:[2]

The issue is faced by me too. When you try saving a file to the sdcard, please make sure that not all the devices have sdcard. So, it will throw FileNotFoundException. So, you might try using a different storage directory.This way you do not need any permission!

Solution 3:[3]

You should first use File.exists() and then File.canRead().

The latter will return false if your app did not create the file.

If your app did not create the file let the user pick the file with ACTION_OPEN_DOCUMENT

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 snachmsm
Solution 2 Sambhav. K
Solution 3 blackapps