您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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) ,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。