Ronny Bigler
Ronny Bigler

Reputation: 724

Environment.getExternalStoragePublicDirectory() in Android 10

how can I get

/storage/emulated/0/Download

in android 10

this method:

Environment.getExternalStoragePublicDirectory()

is deprecated is there an alternative way to get or can I use hardcoded path or is this not recommended?

Upvotes: 0

Views: 6743

Answers (3)

piyushtakale
piyushtakale

Reputation: 1

To get permission to manage files we can also use :

requestPermission(Manifest.permission.MANAGE_EXTERNAL_STORAGE);

This works because android is now asking to manage external storage. after allowing it we can use

Environment.getExternalStorageDirectory();

Upvotes: 0

Jamiul alam khan
Jamiul alam khan

Reputation: 169

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.

https://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

https://developer.android.com/reference/android/provider/MediaStore.html

https://developer.android.com/reference/android/content/Intent.html#ACTION_OPEN_DOCUMENT

Upvotes: 0

Andrew
Andrew

Reputation: 10152

See answer in https://stackoverflow.com/a/59873203/2373819 you will not by default to be able to use file paths outside of you App's private directories, you need to use Storage Access Framework

The blog https://commonsware.com/blog/2020/01/11/scoped-storage-stories-diabolical-details-downloads.html has some good details on Download's

With Download manager you don't have to precheck that it exist's just try and download it and then handle the status result that it failed because it already exists.

Upvotes: 1

Related Questions