我們知道在android7.0,修改了對(duì)私有存儲(chǔ)的限制,導(dǎo)致在獲取資源的時(shí)候,不能通過Uri.fromFile(..)來獲取uri了附井,但是在寫入數(shù)據(jù)的時(shí)候是可以通過Uri.fromFile(..)來獲取uri的,android 官網(wǎng)給出的解決辦法是通過FileProvider來解決這一問題两残,我們需要在AndroidManifest.xml 配制provider節(jié)點(diǎn)永毅。
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"
tools:replace="android:resource"/>
</provider>
其中,provider_paths.xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!-- /storage/emulated/0/Download/${applicationId}/.beta/apk-->
<external-path name="beta_external_path" path="Download/"/>
<!--/storage/emulated/0/Android/data/${applicationId}/files/apk/-->
<external-path name="beta_external_files_path" path="Android/data/"/>
</paths>
但是此時(shí)我們項(xiàng)目中可能會(huì)用到其他一些第三方sdk,為了適配7.0權(quán)限也加了fileprovider,此時(shí)就會(huì)出現(xiàn)沖突問題 : Manifest merger failed人弓,解決這個(gè)問題就需要自定義 FileProvide :
第一步:
自定義fileprovider繼承fileprovider
image.png
第二步:
自定義xml :千萬(wàn)注意filepath指向的路徑也是自定義的卷雕,不要和初始file_paths相同
image.png
第三步:
在manidest 加入provider 注意authorities指向的報(bào)名是工程project的包名
image.png