How Android calls system cameras to save and call system albums

Source: Internet
Author: User
Tags dateformat

The system already has things, if we do not have new requirements, direct call is the most direct. Here's how to call the system camera to take pictures and save pictures and how to call system albums.

First look at the core method of invoking the system camera:

New Intent (mediastore.action_image_capture); Startactivityforresult (camera, camera);

The data returned by the camera is obtained through the following callback method and processed:


public static final int CAMERA = 0x01;

@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { Super. Onactivityresult (Requestcode, ResultCode, data); if(Requestcode = = CAMERA && ResultCode = = Activity.result_ok &&NULL!=data) {String sdstate=environment.getexternalstoragestate (); if(!sdstate.equals (environment.media_mounted)) {GameLog.log (Tag,"SD card Unmount"); return; } NewDateFormat (); String name= Dateformat.format ("Yyyymmdd_hhmmss", Calendar.getinstance (Locale.china)) + ". jpg"; Bundle Bundle=Data.getextras (); //get the data returned by the camera and convert it to a picture formatBitmap Bitmap = (Bitmap) bundle.get ("Data")); FileOutputStream Fout=NULL; File File=NewFile ("/sdcard/pintu/"); File.mkdirs (); String filename=file.getpath () +name; Try{fout=Newfileoutputstream (filename); Bitmap.compress (Bitmap.CompressFormat.JPEG,100, Fout); } Catch(FileNotFoundException e) {e.printstacktrace (); }finally{ Try{Fout.flush (); Fout.close (); } Catch(IOException e) {e.printstacktrace (); } } //Show Pictures }}

Here's how to call a system album and get a Photo:

    1. Intent picture = new Intent (Intent.action_pick,android.provider.mediastore.images.media.external_content_uri);
    2. Startactivityforresult (picture, picture);

The following is the appropriate callback method:

@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {  Super. Onactivityresult (Requestcode, ResultCode, data); if(Requestcode = = CAMERA && ResultCode = = Activity.result_ok &&NULL!=data) {Uri SelectedImage=Data.getdata (); String[] Filepathcolumns={MediaStore.Images.Media.DATA}; Cursor C= This. Getcontentresolver (). Query (SelectedImage, Filepathcolumns,NULL,NULL,NULL);   C.movetofirst (); intColumnIndex = C.getcolumnindex (filepathcolumns[0]); String PicturePath=c.getstring (columnindex);   C.close (); //get the picture and show     }

This completes the system call, very simple.

How Android calls system cameras to save and call system albums

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.