安卓7.0的拍照適配

在Android 7.0以上,在相機(jī)拍照和圖片裁剪上,可能會(huì)碰到以下一些錯(cuò)誤:

Process: com.yuyh.imgsel, PID:22995// 錯(cuò)誤1android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.yuyh.imgsel/cache/1486438962645.jpg exposed beyond app through ClipData.Item.getUri()// 錯(cuò)誤2android.os.FileUriExposedException: file:///storage/emulated/0/DCIM/RxGalleryFinal/IMG_20161018180127.jpg exposed beyond app through Intent.getData()

主要是由于在Android 7.0以后,用了Content Uri 替換了原本的File Uri,故在targetSdkVersion=24的時(shí)候,部分 “`Uri.fromFile()“` 方法就不適用了。 **File Uri 與 Content Uri 的區(qū)別** - File Uri 對(duì)應(yīng)的是文件本身的存儲(chǔ)路徑 - Content Uri 對(duì)應(yīng)的是文件在Content Provider的路徑 所以在android 7.0 以上,我們就需要將File Uri轉(zhuǎn)換為 Content Uri。具體轉(zhuǎn)換方法如下:

```

/** * 轉(zhuǎn)換 content:// uri *? *@paramimageFile *@return*/publicUri getImageContentUri(File imageFile) {? ? String filePath = imageFile.getAbsolutePath();? ? Cursor cursor = getContentResolver().query(? ? ? ? ? ? MediaStore.Images.Media.EXTERNAL_CONTENT_URI,newString[] { MediaStore.Images.Media._ID },? ? ? ? ? ? MediaStore.Images.Media.DATA +"=? ",newString[] { filePath },null);if(cursor !=null&& cursor.moveToFirst()) {? ? ? ? int id = cursor.getInt(cursor? ? ? ? ? ? ? ? .getColumnIndex(MediaStore.MediaColumns._ID));? ? ? ? Uri baseUri = Uri.parse("content://media/external/images/media");returnUri.withAppendedPath(baseUri,""+ id);? ? }else{if(imageFile.exists()) {? ? ? ? ? ? ContentValues values =newContentValues();? ? ? ? ? ? values.put(MediaStore.Images.Media.DATA, filePath);returngetContentResolver().insert(? ? ? ? ? ? ? ? ? ? MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);? ? ? ? }else{returnnull;? ? ? ? }? ? }}

```

那么,我們?cè)诓眉舻臅r(shí)候,應(yīng)該如下調(diào)用:


privatevoid crop(String imagePath) {? ? File file =newFile("xxx.jpg");? ? cropImagePath = file.getAbsolutePath();? ? Intent intent =newIntent("com.android.camera.action.CROP");? ? intent.setDataAndType(getImageContentUri(newFile(imagePath)),"image/*");? ? intent.putExtra("crop","true");? ? intent.putExtra("aspectX", config.aspectX);? ? intent.putExtra("aspectY", config.aspectY);? ? intent.putExtra("outputX", config.outputX);? ? intent.putExtra("outputY", config.outputY);? ? intent.putExtra("scale",true);? ? intent.putExtra("return-data",false);? ? intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));? ? intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());? ? intent.putExtra("noFaceDetection",true);? ? startActivityForResult(intent, IMAGE_CROP_CODE);}


這樣就解決了裁剪的問題,但是!!拍照的時(shí)候就會(huì)出現(xiàn)以下錯(cuò)誤:

Intent cameraIntent =newIntent(MediaStore.ACTION_IMAGE_CAPTURE);if(cameraIntent.resolveActivity(getActivity().getPackageManager()) !=null) {? ? tempFile =newFile(FileUtils.createRootPath(getActivity()) +"/"+ System.currentTimeMillis() +".jpg");? ? LogUtils.e(tempFile.getAbsolutePath());? ? FileUtils.createFile(tempFile);? ? cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));? ? startActivityForResult(cameraIntent, REQUEST_CAMERA);}

android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.yuyh.imgsel/cache/1486438962645.jpg exposed beyond app through ClipData.Item.getUri()

這是因?yàn)榕恼沾鎯?chǔ)的文件,也需要以Content Uri的形式,故采用以下辦法解決:

Step.1

修改AndroidManifest.xml


Step.2

在res/xml/下新建provider_paths.xml文件


Step.3

修改拍照時(shí)的參數(shù)

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getActivity(),BuildConfig.APPLICATION_ID +".provider", tempFile));//Uri.fromFile(tempFile)

搞定!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 只簡(jiǎn)述我發(fā)現(xiàn)問題的根源,有些是適配了7.0,會(huì)報(bào)權(quán)限失敗問題,那是由于沒有動(dòng)態(tài)授權(quán)導(dǎo)致,接下來我一步一步給大家實(shí)現(xiàn)...
    Wocus閱讀 2,395評(píng)論 4 5
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 6,523評(píng)論 0 17
  • 原文地址 : 相機(jī)啟動(dòng)適配 感謝大神的代碼,非常好用! 1.現(xiàn)象 在項(xiàng)目中調(diào)用相機(jī)拍照和錄像的時(shí)候,android...
    Override_6d4c閱讀 1,371評(píng)論 0 1
  • Intent應(yīng)該算是Android中特有的東西。你可以在Intent中指定程序要執(zhí)行的動(dòng)作(比如:view,edi...
    Ten_Minutes閱讀 3,608評(píng)論 0 6
  • 文/陳雄輝 如果知道明天是自由的 岸邊的泅渡客會(huì)釋放多少風(fēng)云? 而今天,從未越過那灣水域 飲盡一片月色后,夜依然渾...
    情島漁夫閱讀 265評(píng)論 0 2