Unable to access File.OpenRead in Android-11

Raghavendra G R 121 Reputation points
2021-12-04T17:40:57.837+00:00

I was getting the image stream using "File.OpenRead(ImagePath)" which was working fine till 'Target android Version= Android 10 (Api level 29)', Now its mandatory to support 'Target android Version= Android 11 (Api level 30)' and unfortunately the File access is not working.

Any Xamarin android developers faced similar issue and found a solution please help.

Regards,
Raghavendra

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Alessandro Caliaro 4,181 Reputation points
    2021-12-05T21:41:44.653+00:00

    External Storage is Forbidden in A11, and with MANAGE_EXTERNAL_STORAGE you risk not to be accepted in the playstore.
    Move your files in the "AppDataDirectory" of your app. You can use file-system-helpers

    1 person found this answer helpful.
    0 comments No comments

  2. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,571 Reputation points Microsoft Vendor
    2021-12-05T02:06:52.963+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You no longer have access by default to arbitrary locations on external storage or removable storage on Android 10 or later.

    For Android 10 and 11, you can add android:requestLegacyExternalStorage="true" to your <application> element in the manifest. This opts you into the legacy storage model, and your existing external storage code will work.

    I recommend you to use following path, you can store image files into your own application's external storage (here is path :"/storage/emulated/0/Android/data/com.companyname.xxx/filesfilename.png")

    Or you can grand the android.permission.MANAGE_EXTERNAL_STORAGE to give access to all files on external storage, except the Android/data and Android/obb directories. To learn more about related Google Play guidelines, read the updated policy from the Policy Help Center.

    And here is link about how to request MANAGE_EXTERNAL_STORAGE permission.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Raghavendra G R 121 Reputation points
    2021-12-07T12:12:11.633+00:00

    Correct. MANAGE_EXTERNAL_STORAGE should have a proper justification else playstore can reject the build.
    I tried below code it worked, not sure if this has some -ve consequences(dint find any as such, still)

    path = System.IO.Path.Combine(Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath, fileName);

    This worked.!

    0 comments No comments