FileProvider NPE null对象引用

时间:2017-06-29 07:02:51

标签: android android-fileprovider

您好我因为FileProvider导致NPE而在启动意图时遇到错误。我按照here提供的步骤操作,但我仍然收到错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference

如果在这里使用provider_paths.xml,我也不确定。到目前为止,这是我的代码:

清单:

<application>... <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider></application>

RES / XML:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

代码:

Uri uri=FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID+".provider", file);

这里似乎是错误的原因。

3 个答案:

答案 0 :(得分:0)

BuildConfig.APPLICATION_ID应该返回您的包名称,但是,在我的情况下,它返回了“ android.support.v4”。我考虑使用getPackagename(),它将正常工作!

所以只需更改

FileProvider.getUriForFile(this,
                            BuildConfig.APPLICATION_ID + ".provider", file);

收件人:

FileProvider.getUriForFile(this,
                            getPackagename() + ".provider", file);

答案 1 :(得分:0)

请参考此链接以获取答案。 enter link description here

首先,这个:

android:authorities="${applicationId}.fileprovider"

您正在使用此

FileProvider.getUriForFile(this,
                            BuildConfig.APPLICATION_ID + ".provider", file);

修改:

FileProvider.getUriForFile(this,
                            BuildConfig.APPLICATION_ID + ".fileprovider", file);

由于我不知道您真正想要的是哪一个,因此我无法提出修复建议。

答案 2 :(得分:-1)

我正在使用它:

清单:

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

XML文件:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="media_folder" path="." />
</paths>

在Java中:

FileProvider.getUriForFile(this,
                                BuildConfig.APPLICATION_ID + ".provider", file);
相关问题