Xamarin forms Android 11 Media Plugin

Matteo Pozzato 116 Reputation points
2021-07-16T13:54:20.147+00:00

Hi,
I have an XF application that targets Android 11 (min 9, target 11).

I have set up the request of permission for external storage like this (following this post on stackoverflow 66366102):

if (DeviceInfo.Version.Major >= 11)
{
bool okReadWrite11 = Android.OS.Environment.IsExternalStorageManager;
if (!okReadWrite11)
{
Intent intent = new Intent(Android.Provider.Settings.ActionManageAppAllFilesAccessPermission);
intent.AddCategory("android.intent.category.DEFAULT");
intent.SetData(Android.Net.Uri.Parse(string.Format("package:{0}", AppInfo.PackageName)));
StartActivityForResult(intent, 2296);
}
[....]

and my AndroidManifest.xml is this one:

<uses-sdk android:minSdkVersion="28" android:targetSdkVersion="30" />
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" />

<application android:label="XXX" android:icon="@mipmap/my_icon" android:requestLegacyExternalStorage="true" android:networkSecurityConfig="@XML /network_security_config">
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@XML /file_paths"></meta-data>
</provider>
</application>
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
</queries>

when I try to get picture from folder or try to get a picture from the camera this error is trigger:

115386-screenshot-2021-07-16-at-154832.png

then I have tried to remove from androidManifest:

android:maxSdkVersion="28"

but this new error is trigger (after I have response "Yes" to the question "allow app XX access to video...."):

115447-screenshot-2021-07-16-at-155220.png

I don't know how to make this works :(

EDIT:
this is the file_paths.xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="FileImportati" path="/FileImportati" />
<external-files-path name="FileAllegati" path="/FileAllegati" />
<external-files-path name="ReportGenerati" path="/ReportGenerati" />

<!--debug-->
<external-path name="ALLEGATI" path="Android/data/it.volos.myApp/ALLEGATI" />
<!--release-->
<!--<external-path name="ALLEGATI" path="data/user/0/it.volos.myApp/files/ALLEGATI" />-->
</paths>

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

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,971 Reputation points
    2021-07-19T06:31:16.103+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    The media plugin has specified the root path of storing the files. For example, the root directory of the photo file is: Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures). We can only use 'Pictures' for the photos and 'Movies' for the videos. So please use the following code to set the path, you could also use . as the path.

       <paths xmlns:android="http://schemas.android.com/apk/res/android">  
         <external-files-path name="my_images" path="Pictures" />  
         <!--<external-files-path name="my_images" path="." />-->  
         <external-files-path name="my_movies" path="Movies" />  
       </paths>  
    

    Check the line code:
    https://github.com/jamesmontemagno/MediaPlugin/blob/ed9fe3cc0d00bf73ab77dd6f087d4e413afe87da/src/Media.Plugin/Android/MediaPickerActivity.cs#L524

    Best Regards,

    Jarvan Zhang


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

    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.

    0 comments No comments

Your answer

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