Android basic----> camera and album calls

Source: Internet
Author: User

Many applications may use the ability to call the camera to take photos and pick pictures from the album, today we start the Android camera with album Call Learning.

Directory Navigation

    1. Call camera to take pictures
    2. Select a photo from the album
    3. Friendship Link

Call camera to take pictures

Approximate process: Call the camera to take a picture, then clip it, and then display it in the ImageView. The project structure is as follows:

First, some of the initialization code:

 Public Static Final intTake_photo = 1; Public Static Final intCrop_photo = 2; Public Static Final intChoose_photo = 3;PrivateImageView picture;PrivateUri Imageuri; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); picture=(ImageView) Findviewbyid (r.id.picture);}

Second, create the file:

// Create a File object to store pictures after taking pictures Private file Createimagefile (String fileName) {    new  file (environment.getexternalstoragedirectory (), fileName);     Try {        if  (outputimage.exists ()) {            outputimage.delete ();        }        Outputimage.createnewfile ();     Catch (IOException e) {        e.printstacktrace ();    }     return outputimage;}

Third, call the system camera to take pictures,

// call the system camera to take a picture and create a JPG file  Public void takephone (view view) {    = Uri.fromfile (Createimagefile ("takephone.jpg"));     New Intent ("Android.media.action.IMAGE_CAPTURE");    Intent.putextra (Mediastore.extra_output, Imageuri);     // Start the camera program }

Iv. Methods of Onactivityresult:

@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {    Switch(requestcode) { CaseTake_photo:phonetake (ResultCode);  Break;  CaseCrop_photo:phonecrop (ResultCode);  Break;  CaseChoose_photo:phonechoose (ResultCode, data);  Break; default:             Break; }}

Five, Phonetake method:

 //  camera callback   void  phonetake (int   ResultCode) { if  (resultcode == Result_ OK) {Intent Intent  = new  Intent ("Com.android.camera.action.CROP" );        Intent.setdataandtype (Imageuri,  "image/*" );        Intent.putextra ( scale, true  );        Intent.putextra (Mediastore.extra_output, Imageuri); Startactivityforresult (Intent, Crop_photo);      //  start clipping program  

Vi. Phonecrop method, clipping callback

//clipped callbacksPrivate voidPhonecrop (intResultCode) {    if(ResultCode = =RESULT_OK) {        Try{bufferedinputstream InputStream=NewBufferedinputstream (Getcontentresolver (). Openinputstream (Imageuri)); Bitmap Bitmap=Bitmapfactory.decodestream (InputStream); Picture.setimagebitmap (bitmap); //show the cropped photo}Catch(FileNotFoundException e) {e.printstacktrace (); }    }}

Vii. Declaration of authority:

<android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>  <android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>

Select a photo from the album

One, select photos from the album:

// Select a photo from the album:  Public void choosephone (view view) {    = Uri.fromfile (Createimagefile ("choosephone.jpg"));     New Intent (intent.action_get_content);    Intent.settype ("image/*");    Intent.putextra (true);    Intent.putextra (true);    Startactivityforresult (Intent, Choose_photo);}

Second, select a picture from the album, and then show

//Select a picture from the album and then showPrivate voidPhonechoose (intResultCode, Intent data) {    if(ResultCode = =RESULT_OK) {Contentresolver resolver=Getcontentresolver (); Uri Originaluri=Data.getdata (); Try{Bitmap photo=MediaStore.Images.Media.getBitmap (resolver, Originaluri); if(Photo! =NULL) {picture.setimagebitmap (photo); }        } Catch(IOException e) {e.printstacktrace (); }    }}

Friendship Link

Android basic----> camera and album calls

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.