欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android照相、相冊獲取圖片剪裁報(bào)錯(cuò)的解決方法

 更新時(shí)間:2014年11月01日 13:18:48   投稿:mdxy-dxy  
最近在項(xiàng)目中用到了照相和相冊取圖剪裁上傳頭像,就在網(wǎng)上逛了逛,基本都是千篇一律,就弄下來用了用,沒想到的是各種各樣的奇葩問題就出現(xiàn)了。先給大家看看代碼問題慢慢來解決

這是調(diào)用相機(jī) 

	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;
	}

在這里我返回了一個(gè)file對象,這是應(yīng)為項(xiàng)目中需要,大家可以不必真寫,直接傳一個(gè)Uri對象過來就好了

 

下面是調(diào)用相冊

 

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);
 
	}

當(dāng)然接下來是調(diào)用Activity的OnActivityResult了

 

 

	@Override
	protected 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) {//為了取消選取不報(bào)空指針用的
					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;
			}
		}

當(dāng)然還有大家關(guān)心的剪切

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/*");
		// 下面這個(gè)crop=true是設(shè)置在開啟的Intent中設(shè)置顯示的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 是裁剪圖片寬高,切忌不要再改動(dòng)下列數(shù)字,會卡死
		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);//設(shè)置為不返回?cái)?shù)據(jù)
 
		((Activity) context).startActivityForResult(intent, REQUE_CODE_CROP);
	}

在很多博客中都把“return-data”設(shè)置為了true然后在onActivityResult中通過data.getParcelableExtra("data")來獲取數(shù)據(jù),不過這樣的話dp這個(gè)變量的值就不能太大了,不然你的程序就掛了。這里也就是我遇到問題的地方了,在大多數(shù)高配手機(jī)上這樣用是沒有問題的,不過很多低配手機(jī)就有點(diǎn)hold不住了,直接就異常了,包括我們的國產(chǎn)神機(jī)米3也沒能hold住,所以我建議大家不要通過return data 大數(shù)據(jù),小數(shù)據(jù)還是沒有問題的,說以我們在剪切圖片的時(shí)候就盡量使用Uri這個(gè)東東來幫助我們。

下面是我們進(jìn)行剪裁用到的一些參數(shù)

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;
	 }
	 }

相關(guān)文章

最新評論