question

80240195 avatar image
0 Votes"
80240195 asked LeonLu-MSFT commented

take photo and save to Album,but error

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>

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.

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

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.





image.png (41.7 KiB)
image.png (27.5 KiB)
· 9
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.

Thank you very much. With your method, I can call the system camera to take pictures. But strangely, no photos were found in the album.

0 Votes 0 ·

Do you have Google Photo app? If so, I use this code, I find it store temp folder like following screenshot.

125965-image.png


0 Votes 0 ·
image.png (82.9 KiB)

Or you can post your demo, I can help you to make a test.

0 Votes 0 ·

OK, I will post.

0 Votes 0 ·

LeonLu-MSFT:
I have post my code project.
Please help me.
Thank you very much.
Please download it at CartoonCamera.zip


0 Votes 0 ·

If you want to save image your Album, due to the android 11's limitation, we have to access the MANAGE_EXTERNAL_STORAGE permission, when I start the application, I use Snackbar to guide users to grand the permission(click the red settings). to grand the permission

126239-image.png

Then I use code to copy the image to Album. and use fileUpdate method to notify the android system to update the Album, when you choose the image from Album, you can see the image.





0 Votes 0 ·
image.png (34.4 KiB)

To protect your project, can I remove your demo's thread?

0 Votes 0 ·

Thank you.
need not to remove it.

0 Votes 0 ·
Show more comments
80240195 avatar image
0 Votes"
80240195 answered LeonLu-MSFT commented

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?

· 5
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.

Did you try this edited demo?

0 Votes 0 ·

Thank you.
I will try.

0 Votes 0 ·

LeonLu:
Thank for your demo.
I have downloaded it and try.
It can pass building.
But run error.
Please see the screen shot.

   ![126618-8.png][1]


[1]: /answers/storage/attachments/126618-8.png

0 Votes 0 ·
8.png (182.7 KiB)

This code to handle the storage limitation for android 11, If you do not run it in android 11, You can remove this part of code. And add android:requestLegacyExternalStorage = "true" attribute in the <application> tag of AndroidManifest.xml

0 Votes 0 ·

If you want to support Android 11 in future, you can add if stance to judege it.

if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.R)
            {
                if (!Android.OS.Environment.IsExternalStorageManager)
                {
                    Snackbar.Make(FindViewById(Android.Resource.Id.Content), "Permission needed!", Snackbar.LengthIndefinite)
                            .SetAction("Settings", new MyOnClickListener(this)).Show();
                }
            }
0 Votes 0 ·
80240195 avatar image
0 Votes"
80240195 answered

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

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.

80240195 avatar image
0 Votes"
80240195 answered LeonLu-MSFT commented

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

· 1
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.

You are welcome.

0 Votes 0 ·