Xamarin Forms Android - replacement for Android.OS.Environment.GetExternalStoragePublicDirectory

WillAutioItrax 211 Reputation points
2021-05-07T01:15:59.09+00:00

I used to be able to use something like:

string pathTo20 = Android.OS.Environment.GetExternalStoragePublicDirectory(
    Android.OS.Environment.DirectoryPictures).AbsolutePath;

and it would result in

   /storage/emulated/0/Pictures

But GetExternalStoragePublicDirectory has been depricated. And I have not found a replacement.

What I want to do is take a picture and copy it to the Pictures directory so that it can be used outside of my app.

Thanks

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. JessieZhang-MSFT 7,721 Reputation points Microsoft External Staff
    2021-05-07T03:02:50.477+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    Use getExternalFilesDir(), getExternalCacheDir(), or getExternalMediaDirs() (methods on Context) instead of Environment.getExternalStorageDirectory() or GetExternalStoragePublicDirectory.
    Check document : https://developer.android.com/reference/android/os/Environment#getExternalStorageDirectory() .

    Or, it can also be able to work with a Uri, then:

    Use ACTION_CREATE_DOCUMENT to get a Uri to a location of the user's choosing, or

    Use MediaStore, ContentResolver, and insert() to get a Uri for a particular type of media (e.g., an image) .

    Note: Android 10 introduced a new storage paradigm for apps called scoped storage which changes the way apps store and access files on a device's external storage. If you target Android 10 (API level 29) or higher, you can opt out of these scoped storage changes on Android 10 and 11 by setting the value of android:requestLegacyExternalStorage to true in your app's manifest file. But this is not a long-term solution, as your targetSdkVersion will need to be 30 or higher .

    For more details, check doc: https://developer.android.com/training/data-storage/use-cases#opt-out-scoped-storage

    You can check the following hot threads to get more usefu information:

    https://stackoverflow.com/questions/57116335/environment-getexternalstoragedirectory-deprecated-in-api-level-29-java
    https://stackoverflow.com/questions/56468539/getexternalstoragepublicdirectory-deprecated-in-android-q

    Best Regards,

    Jessie Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    1 person found this answer helpful.

  2. Douglas Simao 1 Reputation point
    2021-07-05T17:25:05.91+00:00

    Any Update on this? need the same

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.