文件提供程序错误 [英] FileProvider error

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

问题描述

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.

谢谢!

推荐答案

我终于开始工作了!

记住将 provider 标签放在 manifest 文件中的应用程序标签内 - 这是我的错误(我的 provider 标签在应用程序标签的 OUTSIDE 之外),以及您收到此错误的原因,基本上是说找不到定义提供者的.

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.

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

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