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?