安卓從6.0適配到7.0以上遇到的FileProvider相關問題踩坑


安卓7.0針對應用內部文件權限做了安全性限制,如果外部應用想要讀取內部文件,需要通過fileprovider的形式生成臨時URI給外部應用

最典型的應用是應用內檢查更新,涉及到下載-調用系統安裝器(系統安裝器相對來說也就是其他應用)安裝下載好的APK

首先AndroidManifest.xml里application節點里添加:

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

然后在XML下增加file_paths.xml(這里我只把升級的apk下載到data/data/包名/files/download/目錄下,所以這里只列了這一個目錄)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <files-path path="/download/" name="download"/>
</resources>

在這里有可能會遇到Failed to find configured root that contains xxxxx的問題,其實是這個xml里的filepath是有對應關系的,要根據你文件的實際位置來確定。

比如你的文件下載到data/data/包名/cache目錄下,那你就應該用這個:

<cache-path name="/download/" path="download" />

xml目錄對應path類的目錄關系如下:

<files-path path="path" name="name" />
<!--    對應Context.getFilesDir返回的路徑:"/data/data/包名/files"-->
 
<cache-path name="name" path="path" />
<!--    對應getCacheDir返回的路徑:“/data/data/包名/cache”-->
 
<external-path name="name" path="path" />
<!--    對應Environment.getExternalStorageDirectory返回的路徑:"/storage/emulated/0"-->
 
<external-files-path name="name" path="path" />
<!--    對應Context.getExternalFilesDir(String)/Context.getExternalFilesDir(null)返回的路徑:"/storage/emulated/0/Android/data/包名/files"-->
 
<external-cache-path name="name" path="path" />
<!--    對應Context.getExternalCacheDir()返回的路徑:"/storage/emulated/0/Android/data/包名/cache"-->

FileProvider准備就緒之后就可以創建URI通過Intent傳給系統安裝器了:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 24) {//適配7.0以上系統的FileProvider
    Uri apkUri =FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", apkfile);///-----ide文件提供者名
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
}else {
    intent.setDataAndType(Uri.fromFile(apkfile),"application/vnd.android.package-archive");
}
context.startActivity(intent);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM