項目運行兩個禮拜了,相機也在7.0以下運行的完美,突然早上同事拿他的7.0手機給我說 這是一個大bug.我一看調用相機直接崩潰。報的錯誤如下圖:
接着我以爲是我的文件路徑錯誤,找了老半天沒發現問題,仔細想想不太可能了。於是乎,開始求助各大網友了。
解決方案:
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 + 出現,當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) 中, 第二個參數就是這個裏面的值。
作者:起個牛逼的暱稱
鏈接: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中進行註冊的:
StrictMode
API(嚴苛模式) 政策禁止在應用向外部公開 file://
URI。如果一項包含文件 URI 的 intent 離開你的應用,則應用出現故障,並出現 FileUriExposedException
異常。content://
URI,並授予 URI 臨時訪問的權限,進行此授權的最簡單方式是使用 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>
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{ }
<!--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>
<?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>
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); }