Android photo upload and photo local display, photo cut upload, select local image cut upload

Source: Internet
Author: User

@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult ( Requestcode, ResultCode, data); if (ResultCode = = RESULT_OK) {switch (requestcode) {case take_picture:// Save the local picture and zoom out to show on the interface bitmap bitmap = Bitmapfactory.decodefile (environment.getexternalstoragedirectory () + "/ Image.jpg "); Bitmap Newbitmap = Imagetools.zoombitmap (Bitmap, bitmap.getwidth ()/scale, bitmap.getheight ()/scale);// Due to bitmap memory consumption, it is necessary to reclaim memory, otherwise it will be reported out of a memory abnormal bitmap.recycle ();//The processed picture is displayed on the interface and saved to the local Iv_image.setimagebitmap ( NEWBITMAP); Imagetools.savephototosdcard (Newbitmap, Environment.getexternalstoragedirectory (). GetAbsolutePath (), String.valueof (System.currenttimemillis ())); Break;case Choose_picture:contentresolver resolver =             Getcontentresolver ();//Photo's original resource address uri Originaluri = Data.getdata (); try {//Use ContentProvider to get the original picture by URI bitmap photo = MediaStore.Images.Media.getBitmap (resolver, originaluri); if (photo! = null) {//To prevent the original picture from being too large to cause a memory overflow, here first the original display, and thenRelease the memory occupied by the original bitmap bitmap smallbitmap = Imagetools.zoombitmap (photo, photo.getwidth ()/scale, photo.getheight ()/scale); /releases the memory occupied by the original image, prevents the out of memory abnormal occurrence photo.recycle (); Iv_image.setimagebitmap (Smallbitmap);}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Break;case Crop:uri uri = null;if (data! = null) {URI = Data.getdata (); System.out.println ("Data");} else {System.out.println ("File"); String fileName = getsharedpreferences ("temp", context.mode_world_writeable). GetString ("Tempname", "" "); URI = Uri.fromfile (New File (Environment.getexternalstoragedirectory (), fileName));} Cropimage (URI, $, crop_picture), break;case crop_picture:bitmap photo = null; Uri Photouri = Data.getdata (); if (Photouri! = null) {photo = Bitmapfactory.decodefile (Photouri.getpath ());}                   if (photo = = null) {Bundle extra = Data.getextras (); if (extra! = null) {photo = (Bitmap) extra.get ("Data"); Bytearrayoutputstream stream = new ByteArrayOutputStream ();            Photo.compress (Bitmap.CompressFormat.JPEG, N, Stream); }}iv_image.setimagebitmap (photo); break;default:break;}}} public void Showpicturepicker (Context Context,boolean iscrop) {Final Boolean crop = Iscrop; Alertdialog.builder Builder = new Alertdialog.builder (context), builder.settitle ("picture source"); Builder.setnegativebutton ( "Cancel", null); Builder.setitems (new string[]{"Photo", "album"}, New Dialoginterface.onclicklistener () {//Type code int request_code;@ overridepublic void OnClick (dialoginterface dialog, int which) {switch (which) {case Take_picture:uri Imageuri = null; String FileName = null;intent opencameraintent = new Intent (mediastore.action_image_capture), if (crop) {Request_code = CR op;//Delete the last temporary file sharedpreferences sharedpreferences = getsharedpreferences ("temp", context.mode_world_writeable); Magetools.deletephotoatpathandname (Environment.getexternalstoragedirectory (). GetAbsolutePath (), Sharedpreferences.getstring ("Tempname", ""));//Save this temporary file name filename = String. ValueOf (System.currenttimemillis ()) + ". jpg"; Editor editor = Sharedpreferences.edit (); Editor.putstring ("Tempname", FileName); Editor.commit ();} else {Request_code = Take_picture;filename = "Image.jpg";} Imageuri = Uri.fromfile (New File (Environment.getexternalstoragedirectory (), fileName)),//Specify Photo save path (SD card), Image.jpg is a temporary file, this image will be replaced after each photo Opencameraintent.putextra (Mediastore.extra_output, Imageuri); Startactivityforresult (Opencameraintent, Request_code); break;case choose_picture:intent openAlbumIntent = new Intent (intent.action_get_content); if (crop) {Request_code = crop;} else {request_code = choose_picture;} Openalbumintent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); Startactivityforresult ( Openalbumintent, Request_code); break;default:break;}}); Builder.create (). Show (); Truncate picture public void Cropimage (URI uri, int outputx, int outputy, int requestcode) {Intent Intent = new Intent ("com.android.ca          Mera.action.CROP ");          Intent.setdataandtype (URI, "image/*"); INtent.putextra ("Crop", "true");          Intent.putextra ("Aspectx", 1);          Intent.putextra ("Aspecty", 1);           Intent.putextra ("Outputx", OUTPUTX);         Intent.putextra ("Outputy", outputy);        Intent.putextra ("OutputFormat", "JPEG");        Intent.putextra ("Nofacedetection", true);      Intent.putextra ("Return-data", true); Startactivityforresult (Intent, requestcode);}
Demo Download

Android photo upload and photo local display, photo cut upload, select local image cut upload

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.