Android 7.0中拍照和圖片裁剪適配的問題詳解
前言
Android 7.0系統(tǒng)發(fā)布后,拿到能升級(jí)的nexus 6P,就開始了7.0的適配。發(fā)現(xiàn)在Android 7.0以上,在相機(jī)拍照和圖片裁剪上,可能會(huì)碰到以下一些錯(cuò)誤:
Process: com.yuyh.imgsel, PID: 22995 // 錯(cuò)誤1 android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.yuyh.imgsel/cache/1486438962645.jpg exposed beyond app through ClipData.Item.getUri() // 錯(cuò)誤2 android.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 * * @param imageFile * @return */ public Uri getImageContentUri(File imageFile) { String filePath = imageFile.getAbsolutePath(); Cursor cursor = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=? ", new String[] { 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"); return Uri.withAppendedPath(baseUri, "" + id); } else { if (imageFile.exists()) { ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.DATA, filePath); return getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } else { return null; } } }
那么,我們?cè)诓眉舻臅r(shí)候,應(yīng)該如下調(diào)用:
private void crop(String imagePath) { File file = new File("xxx.jpg"); cropImagePath = file.getAbsolutePath(); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(getImageContentUri(new File(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); }
這樣就解決了裁剪的問題,但是?。∨恼盏臅r(shí)候就會(huì)出現(xiàn)以下錯(cuò)誤:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) { tempFile = new File(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
<application ...> <provider android:name="android.support.v4.content.FileProvider" android:authorities="{替換為你的包名}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider> </application>
Step.2
在res/xml/下新建provider_paths.xml文件
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="external_files" path="."/> </paths>
Step.3
修改拍照時(shí)的參數(shù)
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getActivity(),BuildConfig.APPLICATION_ID + ".provider", tempFile)); //Uri.fromFile(tempFile)
總結(jié)
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)各位Android開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流。
- Android實(shí)現(xiàn)拍照、選擇圖片并裁剪圖片功能
- Android裁剪圖片為圓形圖片的實(shí)現(xiàn)原理與代碼
- 解決Android從相冊(cè)中獲取圖片出錯(cuò)圖片卻無法裁剪問題的方法
- Android實(shí)現(xiàn)從本地圖庫/相機(jī)拍照后裁剪圖片并設(shè)置頭像
- Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理)
- Android編程實(shí)現(xiàn)調(diào)用系統(tǒng)圖庫與裁剪圖片功能
- android文件上傳示例分享(android圖片上傳)
- Android使用post方式上傳圖片到服務(wù)器的方法
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android實(shí)現(xiàn)圖片裁剪和上傳
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)切換主題及換膚功能示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)切換主題及換膚功能,涉及Android界面布局與樣式動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03android實(shí)現(xiàn)手機(jī)傳感器調(diào)用
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)手機(jī)傳感器調(diào)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04Android ViewPager實(shí)現(xiàn)無限循環(huán)效果
這篇文章主要為大家詳細(xì)介紹了Android ViewPager實(shí)現(xiàn)無限循環(huán)效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03官網(wǎng)項(xiàng)目Jetpack?Startup庫學(xué)習(xí)
這篇文章主要為大家介紹了官網(wǎng)項(xiàng)目Jetpack?Startup庫學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Android ScrollView實(shí)現(xiàn)下拉彈回動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Android ScrollView實(shí)現(xiàn)下拉彈回動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android 活動(dòng)條ActionBar的詳解及實(shí)例代碼
這篇文章主要介紹了Android 活動(dòng)條ActionBar的詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12Android 中Crash時(shí)如何獲取異常信息詳解及實(shí)例
這篇文章主要介紹了Android 中Crash時(shí)如何獲取異常信息詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02