Android photography, photo album to get picture clipping error solution

Source: Internet
Author: User


Recently in the project to use the photo and photo album to clip upload Avatar, on-line stroll around, basically is the same, it was used with the use, did not think of a variety of wonderful problems appeared. Let's take a look at the code problem and solve it slowly.

This is called 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). Startactivityforresult (Intent, Reque_code_camera); return camerafile;}
Here I return a file object, which is supposed to be needed for the project, you can not really write, directly pass a URI object come on just fine

Here is the call album

public static void Getimagefromphoto (context context, int reque_code_photo) {Intent Intent = new Intent (intent.action_pic K, NULL); Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");((Activity). Startactivityforresult (Intent, Reque_code_photo);}
Of course, the next thing to call activity Onactivityresult.

@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = = RESULT_OK) {Swit CH (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) {//In order to deselect the URI that does not report null pointer = 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, baos); headpicstring = new String (Base64.encode (Baos.tobytearray (), 0)); Uploadpic (headpicstring);} Break;default:break;}}
And of course there's a cut that everyone cares about.
public static void Startphotozoom (context context, Uri Uri,int reque_code_crop) {int DP = 5 00;intent Intent = new Intent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI, "image/*");//The following crop= True is set to set the displayed view in intent to be cropped intent.putextra ("crop", "true") Intent.putextra ("scale", true);// Go Black side Intent.putextra ("scaleupifneeded", true);//go to Black side//Aspectx aspecty is the ratio of wide Intent.putextra ("Aspectx", 1);// The output is proportional to the X-direction Intent.putextra ("Aspecty", 1);//Outputx outputy is to cut the width of the picture, should not change the following numbers, will be stuck Intent.putextra ("Outputx", DP);/ Output x-direction pixel 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 do not return data ((Activity) context). Startactivityforresult (Intent, Reque_code_crop);} 

In many blogs, "Return-data" is set to true and then the data is obtained through Data.getparcelableextra ("data") in Onactivityresult, but the value of the DP variable cannot be too large. Or your program will hang up. Here is also the place where I have problems, in most of the high-equipped mobile phone so that the use is no problem, but a lot of low with mobile phone is a little hold, and directly on the abnormal, including our domestic God machine rice 3 can not hold, so I suggest you do not pass the return data big data, Small data is still not a problem, say we cut the picture when we try to use the URI to help us.

Here are some of the parameters we used to trim

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, the provide it a Bitmap (not tested)
Circlecrop String If this string was not NULL, it would provide some circular CR
Mediastore.extra_output ("OUTPUT") Uri Set this URi to a File:///, see example code

Finally, the method of obtaining bitmap by URI is affixed to everyone.

public static Bitmap Getbitmapfromuri (URI Uri,context mcontext) {  try  {   ///read URI of picture   Bitmap Bitmap = MediaStore.Images.Media.getBitmap (Mcontext.getcontentresolver (), URI);   return bitmap;  }  catch (Exception e)  {   e.printstacktrace ();   return null;  } }

Reprint Please specify source: http://blog.csdn.net/hellohhj/article/details/40618763


Android photography, photo album to get picture clipping error solution

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.