take photo and save to Album,but error

钢 屈 371 Reputation points
2021-08-23T13:47:42.507+00:00

I use the codes to take photo and save it.
private async void btnCamera_Clicked(object sender, EventArgs e)
{
var photo = await CrossMedia.Current.TakePhotoAsync(
new StoreCameraMediaOptions
{
SaveToAlbum = true
});
}
But error, vs2022 tell that
System.ArgumentException: 'Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check documentation on how to set this up in your project.'

I have add the info to AndroidManifest.xml, but error too.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.cartooncamera">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
<application android:label="CartoonCamera.Android" android:theme="@STYLE /MainTheme"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
</manifest>

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,631 Reputation points Microsoft Vendor
    2021-08-24T01:31:36.127+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    But error, vs2022 tell that
    System.ArgumentException: 'Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check >documentation on how to set this up in your project.'

    I have add the info to AndroidManifest.xml, but error too.

    No, you did not add <provider> tag like following format to the AndroidManifest.xml.

       <?xml version="1.0" encoding="utf-8"?>  
       <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.startactivitydemo11">  
           <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />  
           <application android:label="StartActivityDemo11.Android" android:theme="@style/MainTheme">  
    
    
          <!--add this part-->  
             <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>  
    
    
    
         <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
         <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  
         <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
         <uses-permission android:name="android.permission.CAMERA" />  
       </manifest>  
    

    Here are steps to add the <provider> tag.

    If you target framework set Android 10 or later.

    125748-image.png

    1 (AndroidX) Add the following to your AndroidManifest.xml inside the <application> tags:

       <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>  
    

    2 Add a new folder called xml into your Resources folder and add a new XML file called file_paths.xml. Make sure that this XML file has a Build Action of: AndroidResource.

    125749-image.png

    3 Add the following code to file_paths.xml:

       <?xml version="1.0" encoding="utf-8" ?>  
         <paths xmlns:android="http://schemas.android.com/apk/res/android">  
           <external-files-path name="my_images" path="Pictures" />  
           <external-files-path name="my_movies" path="Movies" />  
         </paths>  
    

    If you target framework blew Android 10 .

    (Non AndroidX) Add the following to your AndroidManifest.xml inside the <application> tags: Other steps is the same as above AndroidX

       <provider android:name="android.support.v4.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>  
    

    You can read more at: https://developer.android.com/training/camera/photobasics.html

    Best Regards,

    Leon Lu


    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.


3 additional answers

Sort by: Most helpful
  1. 钢 屈 371 Reputation points
    2021-08-26T04:26:40.967+00:00

    I use android 10 api 29.
    I have add MANAGE_EXTERNAL_STORAGE to file.
    I do not find the image by camera take at album.
    How to use fileUpdate?


  2. 钢 屈 371 Reputation points
    2021-08-26T04:26:55.087+00:00

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.cartooncamera">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
    <application android:label="CartoonCamera.Android" android:theme="@STYLE /MainTheme">
    <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>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE " />
    </manifest>

    0 comments No comments

  3. 钢 屈 371 Reputation points
    2021-08-26T11:43:56.313+00:00

    LeonLu-MSFT :
    Thank you very much.
    Your code work well at my android 10 phone.


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.