FileProvider crash - npe attempting to invoke XmlResourceParser on a null String

前端 未结 9 1871
误落风尘
误落风尘 2020-11-28 21:38

This is a part of my manifest:




        
9条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 22:08

    You can do this without hardcoding the package name with an additional benefit of being able to run multiple variants on the same device (think release and debug with applicationIdSuffix, see these issues):

    Based on FileProvider.java:560

    final ProviderInfo info = context.getPackageManager()
            .resolveContentProvider(authority, PackageManager.GET_META_DATA);
    final XmlResourceParser in = info.loadXmlMetaData( //560
            context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
    

    you were using the wrong authority and it didn't find the ContentProvider (info == null).

    Change your manifest to (${applicationId} will be replaced by Manifest Merger)

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

    and

    Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".share", result);
    

    The .share suffix is optional, in case you have a real ContentProvider which is better to have the package name as the authority.

提交回复
热议问题