I need to read a file stored in android root directory(/storage/emulated/0/MyFolder) while launching the app first time.
It was possible till API level 28 by using Environment.getExternalStorageDirectory(), But after I migrated to API level 29, Environment.getExternalStorageDirectory() deprecated and stopped working with below message.
This method was deprecated in API level 29. To improve user privacy, direct access to shared/external storage devices is deprecated. When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps. Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.
Also I tried Context().getExternalFilesDir(null) which was returning the following path only "/storage/emulated/0/Android/data/com.myapp.appid/files/", but I need to access a file in "/storage/emulated/0/" path.
So how to read the file in location "/storage/emulated/0/MyFolder" in legal way when running with target API level 29?
The only way is to ask the user to pick the file with Intent#ACTION_OPEN_DOCUMENT and then it is unlikely that the user will be able to pick that directory any way.
Google's stated intent for this change is to stop applications from making a proliferation of files/folders all over the place and give users control. The concept of files paths outside of your App's private directories no longer exists, you have to use MediaStore or SAF (Storage Access Framework) for non private stuff.
Going forward you will not be able to have your own App's public folder like this (unless on external SD card), you can only store stuff as Video, Image, Audio or Download (All other file types).
Note: that currently a number of Manufacturers on pre Android 10 provide a Document Provider that gives you access to the root of the Primary partition as well as the External SD card, but if you look at what is provided in the plain Android OS emulator images or Google's documentation you will not see a provided for the primary root partition as this would break Google's stated intent for this change.
See https://developer.android.com/training/data-storage/
and more specifically
https://developer.android.com/training/data-storage/shared/documents-files
You can ask the user to select a folder (which will be under the restricted public locations) and then you can programmatically access and check and files in this location. See https://developer.android.com/training/data-storage/shared/documents-files#grant-access-directory
You can ask the user once and then store the permission they have given you to access files/folders with https://developer.android.com/training/data-storage/shared/documents-files#persist-permissions
Note: There is a temporary workaround until Android 11 by enabling legacy mode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With