question

WillAutioItrax avatar image
0 Votes"
WillAutioItrax asked dsimao answered

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

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


dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JessieZhang-2116 avatar image
1 Vote"
JessieZhang-2116 answered JessieZhang-2116 commented

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JessieZhang-2116 Thanks for the reply. It made for interesting reading. I followed several of the ideas that you presented but I still haven't found what I'm looking for.

I tried variations on

 var f3 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

and
var t = Context...

But no luck.

One plan is to hard code /storage/emulated/0/Pictures but that feels dirty somehow.

I need a way in Xamarin Forms Android to get back the path to the Gallery so that I can copy images to there and then the user can email them (or whatever).

If the answer was in the post, sorry, I missed it.

Thanks!

0 Votes 0 ·

Could you please post a basic demo to github or onedriver so that we can test on our side?

0 Votes 0 ·
dsimao avatar image
0 Votes"
dsimao answered

Any Update on this? need the same

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.