android photography album access to image clipping error solution

  • 2020-06-12 10:40:22
  • OfStack

This is calling the camera


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

Here I return 1 file object, because this is needed in the project, you can not really write, just pass 1 Uri object

Here is the call album


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

And then, of course, OnActivityResult calling Activity


	@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) {// To unselect a pointer that does not report 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;
			}
		}

And, of course, the shearing that you care about


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/*");
		//  The following crop=true It's set on Intent Set to display VIEW Can be cut 
		intent.putExtra("crop", "true");
		intent.putExtra("scale", true);//  Go to the dark side 
		intent.putExtra("scaleUpIfNeeded", true);//  Go to the dark side 
		// aspectX aspectY  It's the ratio of width to height 
		intent.putExtra("aspectX", 1);// The output is X Ratio of directions 
		intent.putExtra("aspectY", 1);
		// outputX outputY  Is cropped picture width and height, do not change the following figures, will be stuck 
		intent.putExtra("outputX", dp);// The output X Directional pixels 
		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);// Set to return no data 
 
		((Activity) context).startActivityForResult(intent, REQUE_CODE_CROP);
	}

Many blogs set "ES28en-ES29en" to true and then get the data from onActivityResult via ES32en.getParcelableExtra ("data"), but the value of the variable dp should not be too large or your program will die. Here also is the place where I have a problem, in most of the high-end mobile phone there is no problem with that, but a lot of low with the mobile phone is a bit hold not to live, directly abnormalities, including our domestic man also failed to hold m 3, so I suggest you don't pass return data big data, data is no problem, said to us when shear pictures use Uri this thing as far as possible to help us.

Here are 1 of the parameters that we used to do the cropping

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


Finally, the method of obtaining bitmap through Uri is pasted for everyone


public static Bitmap getBitmapFromUri(Uri uri,Context mContext)
	 {
	 try
	 {
	  //  read uri Where the picture is 
	  Bitmap bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), uri);
	  return bitmap;
	 }
	 catch (Exception e)
	 {
	  e.printStackTrace();
	  return null;
	 }
	 }

Related articles: