天天看點

android 7.0 适配

項目運作兩個禮拜了,相機也在7.0以下運作的完美,突然早上同僚拿他的7.0手機給我說 這是一個大bug.我一看調用相機直接崩潰。報的錯誤如下圖:

android 7.0 适配

接着我以為是我的檔案路徑錯誤,找了老半天沒發現問題,仔細想想不太可能了。于是乎,開始求助各大網友了。

解決方案:

1、(推薦)7.0之後你的app就算有權限,給出一個URI之後手機也認為你沒有權限。

不用修改原有代碼,在Application的oncreate方法中:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
}      

2、在調用相機的時候添加7.0系統的判斷,

/*擷取目前系統的android版本号*/
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Log.e("currentapiVersion","currentapiVersion====>"+currentapiVersion);
if (currentapiVersion<24){
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(pathFile));
    startActivityForResult(intent, TAKE_PICTURE);
}else {
    ContentValues contentValues = new ContentValues(1);
    contentValues.put(MediaStore.Images.Media.DATA, pathFile.getAbsolutePath());
    Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(intent, TAKE_PICTURE);
}      

推薦使用第一種。

參考:https://developer.Android.com/reference/android/support/v4/content/FileProvider.html

android 7.0 + 相機拍照 FileUriExposedException

這個異常隻會在Android 7.0 + 出現,當app使用file:// url 共享給其他app時, 會抛出這個異常。

因為在android 6.0 + 權限需要 在運作時候檢查, 其他app 可能沒有讀寫檔案的權限, 是以google在7.0的時候加上了這個限制。

官方推薦使用 FileProvider 解決這個問題。 

下面我們來看看具體實作步驟。

在manifest.xml檔案添加provider,相機,讀寫檔案權限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<application>
   ...
   <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.android.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>
    ...
</application>
           

心得:exported:要求必須為false,為true則會報安全異常。grantUriPermissions:true,表示授予 URI 臨時通路權限。

注意 android:authorities 裡面的值,在後面使用的getUriForFile(Context, String, File) 中, 第二個參數就是這個裡面的值。

provider标簽裡的 android:name的值是固定的。android:authorities的值要跟我們上面擷取uri的時候一直,這裡我使用的是:包名.fileprovider。exported:要求必須為false,為true則會報安全異常。grantUriPermissions:true,表示授予 URI 臨時通路權限。<meta-data />标簽裡面是用來指定共享的路徑。 android:resource="@xml/file_paths"就是我們的共享路徑配置的xml檔案,

作者:起個牛逼的昵稱

連結:http://www.jianshu.com/p/eaf122537740

來源:簡書

著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

編寫file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="images/"/>
</paths>
           
<files-path name="name" path="path" /> //相當 Context.getFilesDir() + path, name是分享url的一部分

<cache-path name="name" path="path" /> //getCacheDir()

<external-path name="name" path="path" /> //Environment.getExternalStorageDirectory()

<external-files-path name="name" path="path" />//getExternalFilesDir(String) Context.getExternalFilesDir(null)

<external-cache-path name="name" path="path" /> //Context.getExternalCacheDir() 
           

如果有權限,則直接打開系統相機:

/** 
* 打開系統相機 
*/
private void openCamera() {    
   File file = new FileStorage().createIconFile();    
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {        
   imageUri = FileProvider.getUriForFile(MainActivity.this, "com.bugull.cameratakedemo.fileprovider", file);//通過FileProvider建立一個content類型的Uri    
   } else {        
   imageUri = Uri.fromFile(file);    
   }    
   Intent intent = new Intent();    
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {        
   intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加這一句表示對目标應用臨時授權該Uri所代表的檔案    
   }    
   intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設定Action為拍照    
   intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//将拍取的照片儲存到指定URI    
   startActivityForResult(intent, REQUEST_CAPTURE);}

  
   
    
這裡指定儲存圖檔路徑的時候,我們做了一個判斷,如果運作裝置的版本低于7.0,則調用Uri.fromFile(),可以擷取到圖檔的本地真實路徑。否則,就調用FileProvider.getUriForFile(),擷取一個封裝過的uri對象,這是因為從安卓7.0開始,直接使用本地真實路徑被認為是不安全的,會抛出FileUriExposedExCeption異常,而FileProvider則是一種特殊的内容提供其,它使用了和内容提供器類似的機制來對資料進行保護,可以選擇性地将封裝過的Uri共享給外部,進而提高了應用的安全性。

    
上面我們提到了内容提供器,作為安卓四大元件之一,肯定是要在xml中進行注冊的:

   
  
  
       對于面向 Android 7.0以上API的應用,Android架構執行的          StrictMode                 API(嚴苛模式) 政策禁止在應用向外部公開          file://                 URI。如果一項包含檔案 URI 的 intent 離開你的應用,則應用出現故障,并出現          FileUriExposedException                 異常。
                        在Android 7.0以上API要在應用間共享檔案,需要發送一項 
             content://                 URI,并授予 URI 臨時通路的權限,進行此授權的最簡單方式是使用 
             FileProvider                 類。
   
   

   
   
    下面以安裝Apk為例,具體步驟如下:
   
   

   
   
    一:清單檔案中設定FileProvider
   
   

   
   
           
<!--exported:要求必須為false,為true則會報安全異常。-->
<!--grantUriPermissions:true,表示授予 URI 臨時通路權限-->
<!--authorities 元件辨別,按照江湖規矩,都以包名開頭${APPLICATION_ID}.fileprovider,避免和其它應用發生沖突,當然也可以自己随便寫-->

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.winterrunner.vandroid70_install_bug.fileprovider"
    android:grantUriPermissions="true"
    android:exported="false">
    <!--中繼資料-->
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />

</provider>
      
TIPS: 注意上面的布局,一般情況下,這樣布局即可。 但是,如果應用中引用的jar或者module也使用了name=“ android.support.v4.content.FileProvider ”,這樣問題      就來了,運作時我們發現會報錯,multiple error,怎麼辦??? 原因:一個應用隻能有一個FileProvider 解決辦法:有兩個解決辦法 -------方法一:就是覆寫jar或者module的provider,然後把file_paths的内容複制到自己寫的file_paths裡面一份,有的甚至還要去               改動源碼,個人感受繁瑣的很。具體實作,請自行百度。 -------方法二:出現這個錯誤的根本就是不能出現兩個相同名字的類,我們自己換個名字不就行了,繼承FileProvider即可 步驟: 1.寫個類MyFileProvider繼承FileProvider,ok
package com.winterrunner.vandroid70_install_bug;

import android.support.v4.content.FileProvider;

/**
 * Created by L.K.X on 2017/5/9.
 */

public class MyFileProvider extends FileProvider{

}      
      2.清單檔案修改為:
<!--exported:要求必須為false,為true則會報安全異常。-->
<!--grantUriPermissions:true,表示授予 URI 臨時通路權限-->
<!--authorities 元件辨別,按照江湖規矩,都以包名開頭${APPLICATION_ID}.fileprovider,避免和其它應用發生沖突,當然也可以自己随便寫-->

<provider
    android:name=".MyFileProvider"
    android:authorities="com.winterrunner.vandroid70_install_bug.fileprovider"
    android:grantUriPermissions="true"
    android:exported="false">
    <!--中繼資料-->
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />

</provider>

      
             二:res建立xml包,建立file_paths.xml檔案
android 7.0 适配
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <paths>
        <external-path
            name="download"
            path="." />
    </paths>
</resources>      
三:調用,注意判斷Android版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    //參數1 上下文, 參數2 Provider主機位址 和配置檔案中保持一緻   參數3  共享的檔案
    Uri apkUri = FileProvider.getUriForFile(context, "com.winterrunner.vandroid70_install_bug.fileprovider", uriFile);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    // 由于沒有在Activity環境下啟動Activity,設定下面的标簽
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    //添加這一句表示對目标應用臨時授權該Uri所代表的檔案
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
    context.startActivity(intent);
}else {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(uriFile), "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}