Android 7.0+調(diào)用其他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.

主要步驟:

  1. 定義一個(gè)FileProvider
  2. 指定可用文件
  3. 檢索文件的內(nèi)容URI
  4. 授予URI的臨時(shí)權(quán)限
  5. 將內(nèi)容URI提供給其他應(yīng)用程序

官方文檔鏈接:(https://developer.android.com/reference/android/support/v4/content/FileProvider.html#ServeUri)

定義一個(gè)FileProvider

  1. 首先需要在AndroidManifest.xml里申請(qǐng)關(guān)于文件的權(quán)限
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

或者

   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  1. 同樣,在AndroidManifest.xml中添加以下代碼,允許授權(quán)臨時(shí)訪問文件

    <manifest>
        ...
        <application>
            ...
            <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="com.mydomain.fileprovider"
                android:exported="false"
                android:grantUriPermissions="true">
                ...
            </provider>
            ...
        </application>
    </manifest>  
    

指定可用文件

  1. res 下新建一個(gè)xml文件夾,并新建一個(gè) filepath.xml 文件,使用 paths 指定具體文件路徑,

pathsname 屬性是用于之后的調(diào)用,path 屬性對(duì)應(yīng)我們需要的文件目錄在對(duì)應(yīng)父節(jié)點(diǎn)的路徑

示例如下

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-cache-path  name="external_storage" path="docs/"/>
</paths>
  1. 創(chuàng)建后需要在fileprovider中加入該xml文件
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.example.yyyyz.systonpad.fileprovider"
    android:grantUriPermissions="true"
    android:exported="false">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

? 在 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ù)

<files-path name="name" path="path" />

? 等價(jià)于使用 Context.getFilesDir()

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

? 等價(jià)于使用 getCacheDir()

<external-path name="name" path="path" />

? 等價(jià)于使用 Environment.getExternalStorageDirectory().

<external-files-path name="name" path="path" />
等價(jià)于使用   `Context#getExternalFilesDir(String) Context.getExternalFilesDir(null)`.
<external-cache-path name="name" path="path" />

? 等價(jià)于使用 Context.getExternalCacheDir().

<external-media-path name="name" path="path" />

? 等價(jià)于使用 Context.getExternalMediaDirs(). 需要在api21+中使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,937評(píng)論 25 709
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,319評(píng)論 0 17
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,538評(píng)論 19 139
  • 在書中的最后寫道“他上了二級(jí)平臺(tái),沿著鐵路線急速地向東走去。他遠(yuǎn)遠(yuǎn)地看見,頭上包著紅紗巾的惠英,胸前飄著紅領(lǐng)巾的明...
    松果閱讀 740評(píng)論 0 3
  • 蘇州,古稱吳,簡(jiǎn)稱為蘇,又稱姑蘇、平江等。自古就被文人稱作人間天堂,也有云:上有天堂,下有蘇杭 的美譽(yù)...
    一馬當(dāng)先乎閱讀 857評(píng)論 11 9

友情鏈接更多精彩內(nèi)容