Android photography and photo album image cropping error Solution

Source: Internet
Author: User

Android photography and photo album image cropping error Solution

 

Recently, I used photos and photo albums to take pictures and cut and upload pictures. I went online for a stroll. They were all the same and I used them, what I did not expect was a variety of strange issues. Let's take a look at the code to solve the problem.

This is to call 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 a file object, which should be needed in the project. You don't have to write it. Just upload a Uri object directly.

 

The following is an example of calling an 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);}
Of course, the next step is to call the OnActivityResult of the Activity.

 

 

@ 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) {// uri = data for unreporting NULL pointer to be deselected. 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 ;}}
Of course there are cut-offs that everyone cares 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 crop = true is set to crop the display VIEW in the enabled Intent. putExtra ("crop", "true"); intent. putExtra ("scale", true); // remove the black intent. putExtra ("scaleUpIfNeeded", true); // remove the Black edge // aspectX aspectY is the width-to-height ratio intent. putExtra ("aspectX", 1); // The output is a proportional intent in the X direction. putExtra ("aspectY", 1); // outputX outputY: Specifies the width and height of the cropped image. Do not change the following numbers. intent will be stuck. putExtra ("outputX", dp); // output the pixel intent in the X direction. 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 not return data (Activity) context ). startActivityForResult (intent, REQUE_CODE_CROP );}

In many blogs, "return-data" is set to true and data is used in onActivityResult. getParcelableExtra ("data") to obtain data. However, the value of the dp variable cannot be too large. Otherwise, your program will be suspended. This is where I encountered the problem. It is no problem to use it on most high-end mobile phones. However, many low-end mobile phones cannot hold the phone, and the exception is thrown, our native host Mi 3 is also unable to hold, so I suggest you do not use the return data big data. Small data is still okay, we try to use Uri to help us when cutting images.

The following are some of the parameters used for tailoring.

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 Shocould 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, paste the method for obtaining bitmap through Uri

 

Public static Bitmap getBitmapFromUri (Uri uri, Context mContext) {try {// read the image where the uri is located Bitmap bitmap = MediaStore. images. media. getBitmap (mContext. getContentResolver (), uri); return bitmap;} catch (Exception e) {e. printStackTrace (); return null ;}}




 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.