简体   繁体   中英

Xamarin Plugin.Media “Unable to get file location”

Very odd case. My xml files are set up exactly the way the readme says it should be. Yet when I try to access CrossMedia.Current.TakePhotoAsync(), I get the following Exception:

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.

But, here's my 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.clipidapp" android:installLocation="preferExternal">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
    <application android:label="ClipIDApp.Android">
        <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.companyname.clipidapp.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" />
</manifest>

And here's file_paths

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

And here's the code where I try to access the TakePhotoAsync() function

try {
    _mediaFile = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
    {
        Directory = "temp",
        SaveToAlbum = false,
        CompressionQuality = 75,
        CustomPhotoSize = 50,
        PhotoSize = PhotoSize.MaxWidthHeight,
        MaxWidthHeight = 2000,
        DefaultCamera = CameraDevice.Front,
        Name = "test.jpg"
    });
    if (_mediaFile == null) return;
    image.Source = ImageSource.FromStream(() =>
    {
        var stream = _mediaFile.GetStream();
        return stream;
    });
}
catch(Exception Ex) 
{
    await Current.MainPage.DisplayAlert("Error", Ex.Message, "OK");
}

Take a look at the answer here

Quote

@ Kevelop said:

When i use below file_paths.xml file (exactly as mentioned in the mediaPlugin README!)

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

It works only when the default location for memory in Android settings is set to internal memory. When set to SD-Card IT DOES NOT WORK !

When i change file_paths.xml file to this:

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

It works for BOTH internal memory and SD-Card.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM