FileProvider错误 [英] FileProvider error

查看:78
本文介绍了FileProvider错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://developer.android.com/training/camera/photobasics.html

我要做的就是用相机拍照,保存并在ImageView中显示.

All I am trying to do is take a picture with the Camera, save it and display it in an ImageView.

我遵循了上面的android教程,并不断在网上收到错误消息(NullPointerException):

I followed the android tutorial above and keep getting an error (a NullPointerException) on the line:

Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider", photoFile);

我知道我需要在应用清单中配置 FileProvider ,并且权限"必须匹配.我不太了解应该在权威参数中添加什么.我复制了教程中的所有代码,包括文件res/xml/file_paths.xml.如有必要,问任何问题.

I know I need to configure the FileProvider in my app's manifest and the "authorities" has to match. I don't quite understand what I should be putting in the authorities arguments. I copied all of the code from the tutorial including the file res/xml/file_paths.xml. Ask any questions if necessary.

谢谢!

推荐答案

我终于使它起作用了!

请记住将提供程序标签放在应用程序标签的清单文件中-这是我的错误(我的提供程序标签在应用程序标签的外面),以及出现此错误的原因,该错误基本上表明它找不到定义提供者.

Remember to put the provider tag INSIDE the application tag in the manifest file - that was my mistake (my provider tag was OUTSIDE of the application tag), and the reason you get this error, which basically says it cannot find the definition of the Provider.

还要确保您在.xml文件中具有正确的路径.这是我的版本:

Also make sure you have the correct paths in the .xml file. Here is my version:

<?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/org.projects.cameraapp/files/Pictures" />

</paths>

当然,您必须更改自己的应用程序的路径.

Of course you have to change the path for your own application.

然后我的实际提供者如下:

My actual Provider then looks like this:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="org.projects.cameraapp.fileprovider"          
    android:exported="false"
    android:grantUriPermissions="true">

    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />

</provider>

同样,您需要在自己的应用程序中更改授权值.

Again, you'll need to change the authorities value in your own application.

您可以从我的原始问题中查看GitHub存储库中的所有源代码.

You can see all the source at the GitHub repository from my original question.

这篇关于FileProvider错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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