项目中配置FileProvider,运行报错android.content.res.XmlResourceParser错误。
ava.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583) at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557) at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)
Debug调试
根据报错的代码,最后锁定在FileProvider#parsePathStrategy()中,其源码如下:
private static PathStrategy parsePathStrategy(Context context, String authority)
throws IOException, XmlPullParserException {
final SimplePathStrategy strat = new SimplePathStrategy(authority);
final ProviderInfo info = context.getPackageManager()
.resolveContentProvider(authority, PackageManager.GET_META_DATA);
final XmlResourceParser in = info.loadXmlMetaData(
context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
if (in == null) {
throw new IllegalArgumentException(
"Missing " + META_DATA_FILE_PROVIDER_PATHS + " meta-data");
}
// ............
return strat;
}
debug调试发现,源码中的info对象为空,因此推断出authority有问题,没有找到。既然authority不存在,肯定是配置有问题。
查找配置
先查看AndroidManifest.xml中FileProvider配置:
<!-- FileProvider配置访问路径,适配7.0及其以上 -->
<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>
在查看调用时候,在 FileProvider.getUriForFile()中传入的authorities:
FileProvider.getUriForFile(context.getApplicationContext(), BuildConfig.APPLICATION_ID+".provider",new File(picturePath))
这就奇怪了,感觉没错呀。 这时候,很大可能就是导入BuildConfig错误了,查看导入包。
package xingencom.easypermissiondemo.utils;
import android.support.v4.BuildConfig;
果然如此,错误导入v4包下的BuildConfig。这导致配置的authority与传入的authority不一样,当然会报错的啦。
正确的做法:
导入自己项目的包下的BuildConfig。
//导入自己项目的包
import com.xingen.easypermissiondemo.BuildConfig;
FileProvider.getUriForFile(context.getApplicationContext(), BuildConfig.APPLICATION_ID+".provider",new File(picturePath))