未设置 Xamarin 表单文件提供程序 [英] Xamarin Forms File Provider not set

查看:38
本文介绍了未设置 Xamarin 表单文件提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习 Xamarin.Forms.我目前正在尝试使用 Plugin.Media.CrossMedia 库实现相机功能.

I am currently going through the process of Learning Xamarin.Forms. I am currently attempting to implement Camera functions using the Plugin.Media.CrossMedia library.

在下面实现:

public async Task<ImageSource> StartCamera()
{
    await CrossMedia.Current.Initialize();


    if (Plugin.Media.CrossMedia.Current.IsTakePhotoSupported && CrossMedia.Current.IsCameraAvailable)
    {
            var photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions() { SaveToAlbum=false, SaveMetaData=false});

            if (photo != null)
                return ImageSource.FromStream(() => { return photo.GetStream(); });
            else
                return null;



    }
    else
    {
        return null;
    }
}

但是,在执行TakePhotoAsync"方法时,我收到以下错误.

However upon executing the 'TakePhotoAsync' method I recieve the following error.

System.ArgumentException:无法获取文件位置.这很可能意味着文件提供程序信息未在您的 Android 清单文件中设置.请查看有关如何在您的项目中进行设置的文档.

我曾尝试查找此内容,但无济于事.我的 AndroidManifest.xml 文件目前看起来像这样.

I have tried looking this up but to no avail. My AndroidManifest.xml file looks like this at the moment.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="RCPTTracker.Android" android:versionCode="1" android:versionName="1.0">
    <uses-sdk />
    <application android:label="RCPTTracker.Android"></application>
</manifest>

我的想法是我需要在此处声明一些内容,但我一直无法找到内容,而且我遵循的所有教程都没有涉及设置文件提供程序的任何内容.

My inkling is that I need to declare something in here, but I haven't been able to find what, and all tutorials i have followed don't go through anything with setting up a File Provider.

有什么建议吗?

推荐答案

自述文件说:

如果您的应用面向 Android N (API 24) 或更新版本,您必须使用 2.6.0+ 版本.

If your application targets Android N (API 24) or newer, you must use version 2.6.0+.

您还必须添加一些额外的配置文件以遵守新的严格模式:

You must also add a few additional configuration files to adhere to the new strict mode:

1.) 将以下内容添加到您的 AndroidManifest.xml 标签内:

1.) Add the following to your AndroidManifest.xml inside the tags:

<provider android:name="android.support.v4.content.FileProvider" 
                android:authorities="YOUR_APP_PACKAGE_NAME.fileprovider" 
                android:exported="false" 
                android:grantUriPermissions="true">
                <meta-data android:name="android.support.FILE_PROVIDER_PATHS" 
                android:resource="@xml/file_paths"></meta-data>
</provider>

YOUR_APP_PACKAGE_NAME 必须设置为您的应用程序包名称!

YOUR_APP_PACKAGE_NAME must be set to your app package name!

2.) 将一个名为 xml 的新文件夹添加到您的 Resources 文件夹中,并添加一个名为 file_paths.xml

2.) Add a new folder called xml into your Resources folder and add a new XML file called 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>

YOUR_APP_PACKAGE_NAME 必须设置为您的应用程序包名称!

YOUR_APP_PACKAGE_NAME must be set to your app package name!

您可以在以下位置阅读更多信息:https://developer.android.com/training/相机/photobasics.html

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

这篇关于未设置 Xamarin 表单文件提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆