android调用另一app的xml,Android 7.0+调用其他App打开文件
? 近期需要用到讀取Android設(shè)備外存中的JSON和Word,在前期開發(fā)時(shí)用于調(diào)試的是Android 6.0的米4,當(dāng)拿到實(shí)際應(yīng)用環(huán)境(Android 7.0)時(shí),Android 6.0之后的版本增加了運(yùn)行時(shí)權(quán)限,應(yīng)用程序在執(zhí)行每個(gè)需要系統(tǒng)權(quán)限的功能時(shí),需要添加權(quán)限請(qǐng)求代碼(默認(rèn)權(quán)限禁止),否則應(yīng)用程序無法響應(yīng);查閱官方文檔后發(fā)現(xiàn)可以使用FileProvider解決該問題
FileProvider概述
官方描述:FileProvider是ContentProvider的一個(gè)特殊的子類,通過創(chuàng)建content:// Uri來替代file:///Uri分享文件給另一個(gè)App,來促進(jìn)安全共享。
原文: FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a content:// Uri for a file instead of a file:/// Uri.
主要步驟:
定義一個(gè)FileProvider
指定可用文件
檢索文件的內(nèi)容URI
授予URI的臨時(shí)權(quán)限
將內(nèi)容URI提供給其他應(yīng)用程序
定義一個(gè)FileProvider
首先需要在AndroidManifest.xml里申請(qǐng)關(guān)于文件的權(quán)限
或者
同樣,在AndroidManifest.xml中添加以下代碼,允許授權(quán)臨時(shí)訪問文件
...
...
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
...
...
指定可用文件
在 res 下新建一個(gè)xml文件夾,并新建一個(gè) filepath.xml 文件,使用 paths 指定具體文件路徑,
在 paths 中 name 屬性是用于之后的調(diào)用,path 屬性對(duì)應(yīng)我們需要的文件目錄在對(duì)應(yīng)父節(jié)點(diǎn)的路徑
示例如下
創(chuàng)建后需要在fileprovider中加入該xml文件
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.yyyyz.systonpad.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
? 在 provider 中加入一個(gè) meta-data 元素 , android:name 屬性設(shè)為以上默認(rèn)值即可, android:resource 設(shè)為剛才所添加的xml文件文件名。
為文件生成Content Uri
在完成以上兩步后即可在程序中引用你所需要的文件,示例如下
File docPath = new File(mContext.getExternalCacheDir(), "docs");
File file = new File(docPath, cadre.getXm() + ".doc");
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
Uri uri = getUriForFile(mContext,
BuildConfig.APPLICATION_ID+ ".fileprovider",
file);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// 設(shè)置文件類型為Word
intent.setDataAndType(uri, "application/msword");
} else{
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/msword");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
? 這里 mContext.getExternalCacheDir() 中獲取的路徑 Android\data\包名\cache 我需要的文件路徑為Android\data\包名\cache\docs\ 所以在 child 中設(shè)為 “docs“ 。
? 之后對(duì)Android系統(tǒng)版本進(jìn)行判斷,若>=7.0,則創(chuàng)建 content://Uri , 否則創(chuàng)建 file:///Uri。
? getUriForFile() 的三個(gè)參數(shù) 第一個(gè)context 無需多說、第二個(gè)參數(shù)為 包名 + .fileprovider , 第三個(gè)參數(shù)為之前創(chuàng)建的要打開的file。
為Uri生成暫時(shí)的Permissions
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
將Uri提供給其他程序
intent.setDataAndType(uri, "application/msword");
mContext.startActivity(intent);
官方文檔中介紹了以下6種不同的路徑以及對(duì)應(yīng)的函數(shù)
? 等價(jià)于使用 Context.getFilesDir()
? 等價(jià)于使用 getCacheDir()
? 等價(jià)于使用 Environment.getExternalStorageDirectory().
等價(jià)于使用 `Context#getExternalFilesDir(String) Context.getExternalFilesDir(null)`.
? 等價(jià)于使用 Context.getExternalCacheDir().
? 等價(jià)于使用 Context.getExternalMediaDirs(). 需要在api21+中使用
總結(jié)
以上是生活随笔為你收集整理的android调用另一app的xml,Android 7.0+调用其他App打开文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 标题图标,android
- 下一篇: android handler,Andr