Android7.0 、8.0拍照、相冊選取照片裁剪照片時閃退問題

Android 7.0、8.0以后的手機,打開手機拍照或者打開相冊時應用會閃退產(chǎn)生錯誤日志:


android.os.FileUriExposedException: ** exposed beyond app through Intent.getData()


解決的方式:在AndroidManifast.xml中添加如下代碼:(具體代碼文末付上)

其中的file_paths需要用戶在res下新建文件夾xml創(chuàng)建file_paths.xml文件代碼如下:(具體代碼文末付上)


注意:紅色部分為自己的包名

在拍照時注意進行判斷sdk版本號即當前手機的版本進行以下設置:



這樣的話使用系統(tǒng)自帶的拍照和選擇相冊功能就不會出現(xiàn)閃退和功能了。

但是有的同學在拍照和選擇照片以后還使用了系統(tǒng)帶的裁剪功能,這樣也會出現(xiàn)閃退,具體的日志就不付了,這是由于Android7.0以后將uri(file)功能轉變了所產(chǎn)生導致的,修復在這個功能只需要一下設置:


方法cropPhoto中uri參數(shù)需要這樣設置才行:

這樣到此就ok了;附上相關代碼:


? ? android:name="android.support.v4.content.FileProvider"

? ? android:authorities="com.ddzn.suyan.fileProvider"

? ? android:exported="false"

? ? android:grantUriPermissions="true">

? ? ? ? android:name="android.support.FILE_PROVIDER_PATHS"

? ? ? ? android:resource="@xml/file_paths" />



<?xml version="1.0" encoding="utf-8"?>

? ? <external-path path="Android/data/com/ddzn/suyan/" name="files_root" />

? ? <external-path path="." name="external_storage_root" />

? ? <external-path name="external_files" path="."/>

</paths>


public void cameraPicker() {

this.imageFile =new File(Settings.TEMP_PATH, UUID.randomUUID() +".jpg");

? ? Intent intent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

? ? if (this.imageFile !=null) {

Uri imageUri;

? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

imageUri = FileProvider.getUriForFile(activity.get(), activity.get().getPackageName() +".fileProvider", imageFile);

? ? ? ? }else {

imageUri = Uri.fromFile(imageFile);

? ? ? ? }

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); // set the image file name

? ? }

intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high

? ? this.activity.get().startActivityForResult(intent, Constants.REQUEST_CODE_CAMERA);

}



/**

* @param uri

* @param width? 裁剪后的寬度

* @param height 裁剪后的高度

*/

public void cropPhoto(Uri uri, int width, int height) {

cropFileName = UUID.randomUUID() +".jpg";

? ? Uri imageUri = Uri.fromFile(new File(Settings.TEMP_PATH, cropFileName));

? ? Intent intent =new Intent("com.android.camera.action.CROP");

? ? intent.setDataAndType(uri, "image/*");

? ? intent.putExtra("crop", "true");

? ? // aspectX aspectY 是寬高的比例

? ? intent.putExtra("aspectX", width);

? ? intent.putExtra("aspectY", height);

? ? // outputX outputY 是裁剪圖片寬高

? ? intent.putExtra("scale", true);// 黑邊

? ? intent.putExtra("scaleUpIfNeeded", true);// 黑邊

? ? intent.putExtra("outputX", width);

? ? intent.putExtra("outputY", height);

? ? intent.putExtra("return-data", false);

? ? intent.putExtra("output", imageUri); // 轉入目標文件

? ? this.activity.get().startActivityForResult(intent, Constants.REQUEST_CODE_CROP);

}



public static UrigetImageContentUri(Context context, File imageFile) {

String filePath = imageFile.getAbsolutePath();

? ? Cursor cursor = context.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 context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

? ? ? ? }else {

return null;

? ? ? ? }

}

}

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容