Xamarin Forms Camera ( Media.Plugin )

I will talk to you about the solution of a mistake I have often taken in this writing recently. Especially recently, we had to give a few more details to include the Media.Plugin, which we used in Xamarin Forms to shoot videos and photos, into the project. If I did not make these settings, my error was as follows.

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.

To avoid such an error, follow the steps below. First, we need to install Media.Plugin with the Manage Nuget Package Manager. After installing this package, we are confronted with a file named readme.txt. After installing the package we need to allow a couple of androids. These permissions will use the camera, read the memory, and write data to the memory. To do this, we have to go to the android layer of our project and click on the right and click on the properties option. There are options on the side of the page. We come to the Android Manifest layer from these options. The permissions granted on this page must be given on the spot above. So we can use these features that we allow.

After completing the first step, we have to take another step, the permissions we give in the Main Activity class. For this you need to copy and paste the following code snippet there.

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
    Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

After passing this step, if the package that we added the other step does not come automatically in the Main Activity class, we need to manually add it. Again, we need to do this with the code snippet below.

Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;

After that, you need to do a few more steps afterwards. We will also add the following line of code to the AssemblyInfo.cs class, which is located under the android layer.

[assembly: UsesFeature("android.hardware.camera", Required = false)]
[assembly: UsesFeature("android.hardware.camera.autofocus", Required = false)]

Finally, we came up with the most critical thing to do. With this last step, we specify which file path we will store our data in. If we can not do it correctly, we get the error I mentioned at the beginning of the article. We will make a few additions to the AndroidManifest.xml file inside the properties under the Android layer. You need to add the following code snippet inside the application tags contained in this file.

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

Lastly, we need to open an xml file to tell the phone what path our files are on. We create this xml file in the Resources folder at the bottom of the Android layer by creating an xml named folder and then creating a file named file_paths.xml into it and importing it into the code below.

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

You can easily use Xaml.Media.Plugin after this process.

You can save the photo here, for taking pictures, selecting or taking a video.

If you have any questions, you can reach us by email or comment.

 

Comments (8)

  • braiansays:

    Monday July 9th, 2018 at 04:46 PM

    Hi, I’m doing an app in Xamarin and I’m throwing the error that you described for your solution but I’m still throwing the same error and I’ve done everything you put in, but there’s no way, I do not know what to modify.

    There is something that catches my attention, the method that was described by those who call it because it seems to me that from some side I could invoke it.

    public override void OnRequestPermissionsResult (int requestCode, string [] permissions, Android.Content.PM.Permission [] grantResults)
    {
         Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult (requestCode, permissions, grantResults);
    }

    thank you very much, discuylpe the inconveniences I am new in this language and it is throwing me many errors

      • braiansays:

        Wednesday August 1st, 2018 at 06:02 PM

        1067/5000
        Traducir del: inglés
        Hi, the error I get when you get to the following code, however I gave you all the permissions that you posted and I get the error “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. ”

        await CrossMedia.Current.Initialize ();
        var opAlma = new StoreCameraMediaOptions ()
        {
        PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
        CustomPhotoSize = 25,
        DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Rear,
        AllowCropping = true,
        SaveToAlbum = true,
        Directory = “Directory”,
        Name = “MyFoto.jpg”,

        };
        //in the following line I throw the error
        var photo = await CrossMedia.Current.TakePhotoAsync (opAlma);
        myFoto.Source = ImageSource.FromStream (() =>
        {
        var stream = foto.GetStream ();
        foto.Dispose ();
        return stream;
        });

        • omersezersays:

          Thursday August 2nd, 2018 at 06:23 PM

          OK. I understand u. Create a folder named xml under the Resources folder. You should also create file_paths.xml in the folder named xml. I solved my problem like that.

  • Furkansays:

    Wednesday July 14th, 2021 at 02:45 AM

    Merhaba
    Ben bir proje üzerinde çalışıyorum. Kameranın içerisinde resim veya animasyon çıkarmam lazım, bir türlü kaynak bulamadım. Yardım ederseniz sevinirim.

    İyi çalışmalar.

    • omersezersays:

      Thursday July 15th, 2021 at 12:42 PM

      Selam,

      Burada custom renderer yazman gerekcektir. Xamarin için sanırım bölye bir destek yok şu an için ama custom renderer ile yapabilirsiniz.

      İyi çalışmalar.

Leave a Reply

Your email address will not be published. Required fields are marked *

Press ESC to close