项目中配置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配置:

          <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))

更多相关文章

  1. android 读取指定路径数据库文件
  2. Android(安卓)内容提供者ContentProvider
  3. Android二个按钮Dialog对话框
  4. Android(安卓)7.0版本升级解析包错误
  5. 调试Android第三方应用所需要的工具
  6. android service
  7. 【android错误收集】android.view.InflateException
  8. android service
  9. eclipse项目导入androidstudio

随机推荐

  1. Android的SDK与ADT不匹配问题
  2. android studio 报Error:failed to find
  3. 环境变量ANDROID_SDK_HOME的作用
  4. android控件之ImageButton android:scale
  5. android布局居中
  6. android:各种访问权限Permission
  7. android消息处理机制学习(一)-Handler,Mess
  8. android 二 BMI 小例子
  9. Android 3.0 r1 API中文文档(107) —— A
  10. android aapt (android asset packaging t