自定义FileProvider继承FileProvider。
1 2
| public class AppleFileProvider extends FileProvider { }
|
AndroidManifest.xml中application节点下添加provider节点。
Provider1 2 3 4 5 6 7 8 9 10 11
| <provider android:name="com.XXX.XXX.sample.AppleFileProvider" android:authorities="${applicationId}.file.provider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/apple_file_provider"/>
</provider>
|
- 多个FileProvider authorities重复
authorities一般是由包名+自定义的标识构成。
Uri uri = FileProvider.getUriForFile(getContext(), context.getPackageName() + “.file.provider”, file);
例子:
AppleFileProvider 和 BoyFileProvider 的authorities重复了,都为${applicationId}.file.provider。
默认会取apk中合并后的AndroidManifest.xml的第一个authorities匹配的FileProvider。
Provider1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <provider android:name="com.XXX.XXX.sample.AppleFileProvider" android:authorities="${applicationId}.file.provider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/apple_file_provider"/>
</provider>
<provider android:name="com.XXX.XXX.sample.BoyFileProvider" android:authorities="${applicationId}.file.provider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/boy_file_provider"/>
</provider>
|
此时Uri uri = FileProvider.getUriForFile(getContext(), context.getPackageName() + “.file.provider”, file);会与AppleFileProvider匹配,也就会去取apple_file_provider.xml中的配置。
如果AppleFileProvider与BoyFileProvider在AndroidManifest的顺序互换一下,那么就会与BoyFileProvider匹配,也就会去取boy_file_provider.xml中的配置。