Android open Local album select picture and Return to display

Source: Internet
Author: User
Tags crop image

. java

1  PackageCom.jerry.crop;2 3 ImportJava.io.File;4 5 Importandroid.app.Activity;6 Importandroid.content.Intent;7 ImportAndroid.graphics.Bitmap;8 ImportAndroid.net.Uri;9 ImportAndroid.os.Bundle;Ten Importandroid.os.Environment; One ImportAndroid.provider.MediaStore; A ImportAndroid.view.View; - ImportAndroid.widget.ImageView; - ImportAndroid.widget.Toast; the  -  Public classMainactivityextendsActivity { -  -     Private Static Final intPhoto_request_carema = 1;//Take Pictures +     Private Static Final intPhoto_request_gallery = 2;//Select from the album -     Private Static Final intPhoto_request_cut = 3;//Results +  A     PrivateImageView iv_image; at  -     /*Avatar name*/ -     Private Static FinalString photo_file_name = "Temp_photo.jpg"; -     PrivateFile tempfile; -  - @Override in     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); to Setcontentview (r.layout.activity_main); +          This. Iv_image = (ImageView) This. Findviewbyid (r.id.iv_image); -     } the  *     /* $ * get from albumsPanax Notoginseng      */ -      Public voidGallery (view view) { the         //Activate the System gallery, select a picture +Intent Intent =NewIntent (Intent.action_pick); AIntent.settype ("image/*"); the         //open an activity with a return value, the request code is photo_request_gallery + Startactivityforresult (Intent, photo_request_gallery); -     } $  $     /* - * get from camera -      */ the      Public voidcamera (view view) { -         //activating the cameraWuyiIntent Intent =NewIntent ("Android.media.action.IMAGE_CAPTURE"); the         //determine if the memory card is usable and can be stored -         if(Hassdcard ()) { WuTempfile =NewFile (Environment.getexternalstoragedirectory (), - photo_file_name); About             //To create a URI from a file $Uri uri =Uri.fromfile (tempfile); - Intent.putextra (Mediastore.extra_output, URI); -         } -         //open an activity with a return value, the request code is Photo_request_carema A Startactivityforresult (Intent, photo_request_carema); +     } the  -     /* $ * Cut Pictures the      */ the     Private voidcrop (Uri uri) { the         //Crop Image Intent theIntent Intent =NewIntent ("Com.android.camera.action.CROP"); -Intent.setdataandtype (URI, "image/*"); inIntent.putextra ("Crop", "true"); the         //the scale of the cropping box, 1:1 theIntent.putextra ("Aspectx", 1); AboutIntent.putextra ("Aspecty", 1); the         //Size of output picture after cropping theIntent.putextra ("Outputx", 250); theIntent.putextra ("Outputy", 250); +  -Intent.putextra ("OutputFormat", "JPEG");//Picture Format theIntent.putextra ("Nofacedetection",true);//Remove Face recognitionBayiIntent.putextra ("Return-data",true); the         //open an activity with a return value, the request code is photo_request_cut the Startactivityforresult (Intent, photo_request_cut); -     } -  the     /* the * Determine if sdcard is being mounted the      */ the     Private BooleanHassdcard () { -         if(Environment.getexternalstoragestate (). Equals ( the environment.media_mounted)) { the             return true; the}Else {94             return false; the         } the     } the 98 @Override About     protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { -         if(Requestcode = =photo_request_gallery) {101             //data returned from the album102             if(Data! =NULL) {103                 //get the full path of the picture104Uri uri =Data.getdata (); the crop (URI);106             }107 108}Else if(Requestcode = =Photo_request_carema) {109             //data returned from the camera the             if(Hassdcard ()) {111 crop (Uri.fromfile (tempfile)); the}Else {113Toast.maketext (mainactivity. This, "No memory card found, no photos saved!" ", 0). Show (); the             } the  the}Else if(Requestcode = =photo_request_cut) {117             //data returned from the cut image118             if(Data! =NULL) {119Bitmap Bitmap = Data.getparcelableextra ("Data")); -                  This. Iv_image.setimagebitmap (bitmap);121             }122             Try {123                 //Delete a temporary file124 Tempfile.delete (); the}Catch(Exception e) {126 e.printstacktrace ();127             } - 129         } the 131         Super. Onactivityresult (Requestcode, ResultCode, data); the     }133}

. xml

1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:orientation= "Vertical"6Tools:context= ". Mainactivity ">7 8<Button9Android:layout_width= "Wrap_content"Tenandroid:layout_height= "Wrap_content" Oneandroid:onclick= "Gallery" Aandroid:text= "Get gallery Picture"/> -<Button -Android:layout_width= "Wrap_content" theandroid:layout_height= "Wrap_content" -android:onclick= "Camera" -android:text= "Photo Capture"/> -  +<ImageView -Android:id= "@+id/iv_image" +Android:layout_width= "Wrap_content" Aandroid:layout_height= "Wrap_content"/> at  -</LinearLayout>

Android open Local album select picture and Return to display

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.