Android7.0 FileUriExposedException 問題解決

來源:互聯網
上載者:User

標籤:ext.get   app   src   utf-8   本地   nal   files   原因   class   

一、 FileUriExposedException 的原因

Android7.0不識別uri以file://開頭,要將其轉換為content://才能識別uri

二、如何解決

1.xml的建立:

file_paths.xml中編寫該Provider對外提供檔案的目錄:檔案放置在res/xml/下。 為了避免和其它app衝突,最好帶上自己app的包名。

檔案內容:

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="." name="external_storage_root" />
</paths>

內部的element可以是files-path,cache-path,external-path,external-files-path,external-cache-path,分別對應Context.getFilesDir(),Context.getCacheDir(),Environment.getExternalStorageDirectory(),Context.getExternalFilesDir(),Context.getExternalCacheDir()等幾個方法。後來翻看源碼發現還有一個沒有寫進文檔的,但是也可以使用的element,是root-path,直接對應檔案系統根目錄。不過既然沒有寫進文檔中,其實還是有將來移除的可能的。使用的話需要注意一下風險。

2.Manifests.xml中配置

<manifest>

   ...

  <application>

     ...

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

    ...

  </application>

</manifest>

3.Uri使用

1)getUriForFile方法轉換:

public static Uri getUriForFile(Context context, File file) {
return FileProvider.getUriForFile(context, GB.getCallBack().getApplicationId(), file);
}

//第二個參數是manifest中定義的`authorities`:因為當時file_paths.xml中賦值為.,故第二個參數是:"com.up366.mobile.fileProvider"

2)intent加Flags

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

註:擷取本地圖片,沒有uri就不用寫

完成。

 

 

 

 

 


Android7.0 FileUriExposedException 問題解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.