Android圆形头像拍照后“无法加载此图片”的问题解决方法(适配Android7.0)
程序员文章站
2023-12-17 23:22:04
feature: 点击选择拍照或者打开相册,选取图片进行裁剪最后设置为圆形头像。
problem: 拍好照片,点击裁剪,弹toast“无法加载此图片”。
sol...
feature: 点击选择拍照或者打开相册,选取图片进行裁剪最后设置为圆形头像。
problem: 拍好照片,点击裁剪,弹toast“无法加载此图片”。
solution: 在裁剪的class里加两行代码
intent.addflags(intent.flag_grant_read_uri_permission);
intent.addflags(intent.flag_grant_write_uri_permission);
主要代码如下:
public static final string photo_image_file_name = "fileimg.jpg"; public static final int camera_request_code = 100; public static final int image_request_code = 101; public static final int result_request_code = 102; private file tempfile = null; private uri imageuri; //跳转相机 private void tocamera() { file outputimage = new file(getactivity().getexternalcachedir(), photo_image_file_name); try { if (outputimage.exists()) { outputimage.delete(); } outputimage.createnewfile(); } catch (ioexception e) { e.printstacktrace(); } if (build.version.sdk_int >= 24) { imageuri = fileprovider.geturiforfile(getactivity(), "com.liushengjie.smartbutler.fileprovider", outputimage); } else { imageuri = uri.fromfile(outputimage); } //启动相机程序 intent intent = new intent("android.media.action.image_capture"); intent.putextra(mediastore.extra_output, imageuri); startactivityforresult(intent, camera_request_code); dialog.dismiss(); } //跳转相册 private void topicture() { intent intent = new intent(intent.action_pick); intent.settype("image/*"); startactivityforresult(intent, image_request_code); dialog.dismiss(); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { if (resultcode != getactivity().result_canceled) { switch (requestcode) { //相机数据 case camera_request_code: //tempfile = new file(environment.getexternalstoragedirectory(), photo_image_file_name); //startphotozoom(uri.fromfile(tempfile)); startphotozoom(imageuri); break; //相册数据 case image_request_code: startphotozoom(data.getdata()); break; case result_request_code: //有可能点击舍弃 if (data != null) { //拿到图片设置 setimagetoview(data); //删除原来的图片 if (tempfile != null) { tempfile.delete(); } } break; } } } //裁剪 private void startphotozoom(uri uri) { if (uri == null) { l.e("uri == null"); return; } intent intent = new intent("com.android.camera.action.crop"); intent.setdataandtype(uri, "image/*"); intent.addflags(intent.flag_grant_read_uri_permission); intent.addflags(intent.flag_grant_write_uri_permission); //裁剪 intent.putextra("crop", true); //宽高比例 intent.putextra("aspectx", 1); intent.putextra("aspecty", 1); //图片质量 intent.putextra("outputx", 320); intent.putextra("outputy", 320); //发送数据 intent.putextra("return-data", true); startactivityforresult(intent, result_request_code); } //设置图片 private void setimagetoview(intent data) { bundle bundle = data.getextras(); if (bundle != null) { bitmap bitmap = bundle.getparcelable("data"); profile_image.setimagebitmap(bitmap); } }
总结
以上所述是小编给大家介绍的android圆形头像拍照后“无法加载此图片”的问题解决方法(适配android7.0) ,希望对大家有所帮助