URGENT!!! MediaPicker.CapturePhotoAsync is throwing the error

Pearl 61 Reputation points
2021-06-07T10:43:55.727+00:00

Hello,
I am trying to capture photo and show it in <image>. But,
MediaPicker.CapturePhotoAsync is throwing the error "Failed to find configured root that contains /data/data/com.companyname.mobapp/cache/2203693cc04e0be7f4f024d5f9499e13/73d8bd33d80e47fa9a6cd550fb460c7d/7fd54417a7b44e5297269ccab9eebcee.jpg"

The code is

private async void btnCapturePhoto_Clicked(object sender, EventArgs e)
    {
        try
        {   
  strInMemPath =System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData) ;             
            var objPhoto = await MediaPicker.CapturePhotoAsync();


            if (objPhoto == null)
                return;
            var objStream = await objPhoto.OpenReadAsync();
             imgPhoto.Source =ImageSource.FromFile(System.IO.Path.Combine(strInMemPath, "Photo.png"))

        }
        catch (Exception ex)
        {

        }

    }

The File_Paths.xml is

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

Can you guys please help? Thanks in advance for the answers.

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

2 answers

Sort by: Most helpful
  1. JarvanZhang 23,951 Reputation points
    2021-06-08T06:33:41.557+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    1.Please make sure the related permissions have been added to the AndroidManifest.xml. The Xamarin.Essentials.MediaPicker api doesn't require the File_Paths.xml. Please check the related doc to get the details: https://learn.microsoft.com/en-us/xamarin/essentials/media-picker?tabs=android

    If you want to caputre a photo and set it as the image's source, try using the ImageSource.FromStream command to do this.

       private async void Button_Clicked(object sender, EventArgs e)  
       {  
           try  
           {  
               var objPhoto = await MediaPicker.CapturePhotoAsync();  
    
               if (objPhoto == null)  
                   return;  
               var objStream = await objPhoto.OpenReadAsync();  
               img.Source = ImageSource.FromStream(() => objStream);  
           }  
           catch (Exception ex)  
           {  
    
           }  
       }  
    

    2.What device and Android sdk do you face the issue? Try to test the code on another emulator or device?

    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

  2. Pearl 61 Reputation points
    2021-06-08T15:14:16.827+00:00

    Hello JarvanZhang-MSFT,
    Thaks a lot for your reply. But,While eliminating the unrelated code i deleted the function call for the file save too.
    The code for button click is
    private async void btnCapturePhoto_Clicked(object sender, EventArgs e)
    {
    try
    {
    strInMemPath =System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData) ;
    var objPhoto = await MediaPicker.CapturePhotoAsync();

             if (objPhoto == null)
                 return;
             var objStream = await objPhoto.OpenReadAsync();
    SaveFile(objStream ,System.IO.Path.Combine(strInMemPath, "Photo.png"));
              imgPhoto.Source =ImageSource.FromFile(System.IO.Path.Combine(strInMemPath, "Photo.png"))
    
         }
         catch (Exception ex)
         {
    
         }
     }
    

    I tried to implement your suggestion. But the execution doesn't even reach the line for imagesource assigning. It throws the error at MediaPicker.CapturePhotoAsync(). The error is "Failed to find configured root that contains /data/data/com.companyname.mobapp/cache/2203693cc04e0be7f4f024d5f9499e13/73d8bd33d80e47fa9a6cd550fb460c7d/7fd54417a7b44e5297269ccab9eebcee.jpg"

    I am using Samsung S7 for the debugging. i tried with Samsung S4 phone too. It doesnt work for both of them.
    Please help. I am stuck. :(


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.