您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。