標籤:android android照相相簿擷取圖片剪裁報錯的
最近在項目中用到了照相和相簿取圖剪裁上傳頭像,就在網上逛了逛,基本都是千篇一律,就弄下來用了用,沒想到的是各種各樣的奇葩問題就出現了。先給大家看看代碼問題慢慢來解決
這是調用相機
public static File getImageFromCamer(Context context, File cameraFile,int REQUE_CODE_CAMERA, Intent intent) {intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);File fileDir = HelpUtil.getFile(context, "/Tour/user_photos");cameraFile = new File(fileDir.getAbsoluteFile() + "/"+ System.currentTimeMillis() + ".jpg");intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile));((Activity) context).startActivityForResult(intent, REQUE_CODE_CAMERA);return cameraFile;}在這裡我返回了一個file對象,這是應為項目中需要,大家可以不必真寫,直接傳一個Uri對象過來就好了
下面是調用相簿
public static void getImageFromPhoto(Context context, int REQUE_CODE_PHOTO) {Intent intent = new Intent(Intent.ACTION_PICK, null);intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");((Activity) context).startActivityForResult(intent, REQUE_CODE_PHOTO);}當然接下來是調用Activity的OnActivityResult了
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode == RESULT_OK) {switch (requestCode) {case ConstantUtil.REQUE_CODE_CAMERA:uri = Uri.fromFile(cameraFile);PhotoUtil.startPhotoZoom(context, uri,ConstantUtil.REQUE_CODE_CROP);break;case ConstantUtil.REQUE_CODE_PHOTO:if (null != data) {//為了取消選取不報null 指標用的uri = data.getData();PhotoUtil.startPhotoZoom(context, uri,ConstantUtil.REQUE_CODE_CROP);}break;case ConstantUtil.REQUE_CODE_CROP:if(uri==null){break;}cropBitmap=HelpUtil.getBitmapFromUri(uri,context);if (cropBitmap != null) {iv_headphoto.setImageBitmap(cropBitmap);baos = new ByteArrayOutputStream();cropBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);headPicString = new String(Base64.encode(baos.toByteArray(), 0));UploadPic(headPicString);}break;default:break;}}當然還有大家關心的剪下
public static void startPhotoZoom(Context context, Uri uri,int REQUE_CODE_CROP) {int dp = 500;Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, "image/*");// 下面這個crop=true是設定在開啟的Intent中設定顯示的VIEW可裁剪intent.putExtra("crop", "true");intent.putExtra("scale", true);// 去黑邊intent.putExtra("scaleUpIfNeeded", true);// 去黑邊// aspectX aspectY 是寬高的比例intent.putExtra("aspectX", 1);//輸出是X方向的比例intent.putExtra("aspectY", 1);// outputX outputY 是裁剪圖片寬高,切忌不要再改動下列數字,會卡死intent.putExtra("outputX", dp);//輸出X方向的像素intent.putExtra("outputY", dp);intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());intent.putExtra("noFaceDetection", true);intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);intent.putExtra("return-data", false);//設定為不返回資料((Activity) context).startActivityForResult(intent, REQUE_CODE_CROP);}
在很多部落格中都把“return-data”設定為了true然後在onActivityResult中通過data.getParcelableExtra("data")來擷取資料,不過這樣的話dp這個變數的值就不能太大了,不然你的程式就掛了。這裡也就是我遇到問題的地方了,在大多數高配手機上這樣用是沒有問題的,不過很多低配手機就有點hold不住了,直接就異常了,包括我們的國產神機米3也沒能hold住,所以我建議大家不要通過return data 大資料,小資料還是沒有問題的,說以我們在剪下圖片的時候就盡量使用Uri這個東東來協助我們。
下面是我們進行剪裁用到的一些參數
Exta Options Table for image/* crop:
| SetExtra |
DataType |
Description |
| crop |
String |
Signals the crop feature |
| aspectX |
int |
Aspect Ratio |
| aspectY |
int |
Aspect Ratio |
| outputX |
int |
width of output created from this Intent |
| outputY |
int |
width of output created from this Intent |
| scale |
boolean |
should it scale |
| return-data |
boolean |
Return the bitmap with Action=inline-data by using the data |
| data |
Parcelable |
Bitmap to process, you may provide it a bitmap (not tested) |
| circleCrop |
String |
if this string is not null, it will provide some circular cr |
| MediaStore.EXTRA_OUTPUT ("output") |
URI |
Set this URi to a File:///, see example code |
最後把通過Uri獲得bitmap的方法給大家貼上
public static Bitmap getBitmapFromUri(Uri uri,Context mContext) { try { // 讀取uri所在的圖片 Bitmap bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), uri); return bitmap; } catch (Exception e) { e.printStackTrace(); return null; } }
轉載請指明出處:http://blog.csdn.net/hellohhj/article/details/40618763
android照相、相簿擷取圖片剪裁報錯的解決方案