Android Open album pick a picture or turn on the camera to take pictures and crop

Source: Internet
Author: User
Tags crop image

The first thing is to open a local album to choose a picture:

Intent albumintent = new Intent (intent.action_pick);//Open the system's album Albumintent.settype ("image/*"); Startactivityforresult (Albumintent, 0x1004);

When you finish selecting the picture, write the data in Onactivityresult:

@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {        if (data==null) {if (isNull) {}else {     if (requestcode==0x1004) {//Select Picture from album Getclipphotobypickpicture (Data.getdata (), Data.getextras ());}}}
get the return data to crop the picture:

/** * Get the returned photo information * @param URI to the URI of the returned data * @param bundle */private void Getclipphotobypickpicture (Uri Uri, Bu) into the returned data Ndle bundle) {if (URI = = null) {/** * */  } else {Intent Intent = new Intent ("Com.android.camera.action.CROP"); intent.se Tdataandtype (URI, "image/*"), Intent.putextra ("crop", "true"), Intent.putextra ("Aspectx", 1);//Aspectx is the ratio of width to height, Here the square is set (aspect ratio is 1:1) Intent.putextra ("Aspecty", 1), Intent.putextra ("Outputx", 400); Outputx Outputy is a cropped picture with a wide height of Intent.putextra ("Outputy", 400); Somehow, I can't set it too large, <640intent.putextra ("scale", true); Intent.putextra (Mediastore.extra_output, URI); Intent.putextra ("Return-data", true); Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection", true); Startactivityforresult (Intent, 0x1006);}}

call the system camera to take pictures and crop:

Boolean isnull=false;//determines whether the camera return data is empty       Intent Getimagebycamera = new Intent ("Android.media.action.IMAGE_CAPTURE") ; File File = new file (environment.getexternalstoragedirectory () + "/elephant/accountimg/accountimg.jpg"); File.exists ()) {File.delete ();} Specifies that the Uri stores the photo Getimagebycamera.putextra (mediastore.extra_output,uri.fromfile (file)); isNull = true; Startactivityforresult (Getimagebycamera, 0x1005);

also write the receive data in the Onactivityresult, but at this time found that the returned data is empty, at this time have to add a judgment:

if (data==null) {if (isNull) {///when the returned data is empty and IsNull is true, the photo taken by the camera is saved as file fileimg = new file ( Environment.getexternalstoragedirectory () + "/elephant/accountimg/accountimg.jpg"); Uri Urifromimg=uri.fromfile (fileimg);//extract uri//crop image Intent Intent = new Intent ("Com.android.camera.action.CROP"); Intent.setdataandtype (urifromimg, "image/*"); Intent.putextra ("Crop", "true"); Intent.putextra ("Aspectx", 1);// The ASPECTX is a wide-height ratio, where the square is set (the aspect ratio is 1:1) Intent.putextra ("Aspecty", 1); Intent.putextra ("Outputx", 400); Outputx Outputy is a cropped picture with a wide height of Intent.putextra ("Outputy", 400); Somehow, I can't set it too large, <640intent.putextra ("scale", true); Intent.putextra (Mediastore.extra_output, urifromimg); Intent.putextra ("Return-data", true); Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection", true); No face Detectionstartactivityforresult (intent, 0x1006); LOG.D ("Logd", "Behind the URI"); isnull=false;}} else {//when Data!=null}

Also written in Onactivityresult, let the cropped picture appear on the ImageView:

if (Requestcode = = 0x1006) {//Receive cropped picture information and save to local folder bitmap Bmap = Data.getparcelableextra ("data"); Iv_show_ Img.setimagebitmap (BMAP);//The image is saved to a file FileOutputStream Foutput = null;try {File Filedir = new file ( Environment.getexternalstoragedirectory () + "/elephant/accountimg/"), if (!filedir.exists ()) {fileDir.mkdirs ();} File fileimg = new file (Filedir, "accountimg.jpg"), if (Fileimg.exists ()) {Fileimg.delete ();} Foutput = new FileOutputStream (fileimg); Bmap.compress (Bitmap.CompressFormat.JPEG, foutput); Compress picture} catch (FileNotFoundException e) {e.printstacktrace ();} finally {if (null! = Foutput) {try {foutput.close ()} CATC H (IOException e) {e.printstacktrace ();}}}}


Reference:

http://blog.csdn.net/beyond0525/article/details/8939984

Http://blog.csdn.net/it_transformers/article/details/42060775#comments






Android Open album pick a picture or turn on the camera to take pictures and crop

Related Article

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.