Two ways Android gets pictures from camera

Source: Internet
Author: User

Method One:

This method will be sent directly to the application by the camera, but the compressed picture is returned, the display is not clear

/** start camera */private void Intentcamera () {try {Intent cameraintent = new Intent (Mediastore.action_image_ca         Pture);     Startactivityforresult (cameraintent, 0);     } catch (Activitynotfoundexception e) {e.printstacktrace (); }}

/** gets the picture in Onactivityresult */private void Getimgfromcamera () {Bundle bundle = Data.getextras ();    BM = (Bitmap) bundle.get ("Data");    if (BM! = null) bm.recycle ();    BM = (Bitmap) Data.getextras (). Get ("data");    if (BM! = null) {IMG.SETIMAGEBITMAP (BM); }}

Method Two:

This method is what you get, but it generates temporary files on the SD card

/***  Open Camera      */private void intentcamera () {     Intent cameraintent = new intent (Android.provider.MediaStore.ACTION_IMAGE_CAPTURE);     file file = new file (Environment.getexternalstoragedirectory ()  +   "/images");     if (!file.exists ()) {         File.mkdirs ();     }    uri muri = uri.fromfile (         new file (Environment.getexternalstoragedirectory ()  +  "/images/",          "Cameraimg"  + string.valueof ( System.currenttimemillis ())  +  ". jpg");     cameraintent.putextra ( Android.provider.mediastore.extra_output, muri);     cameraintent.putextra (" Return-data ",  true);     sTartactivityforresult (cameraintent, 1);} 

/***  gets the data returned by the camera       */private void getimgfromcamera () {             ContentResolver cr =  This.getcontentresolver ();     try {        if (camerabitmap != null)               Camerabitmap.recycle ();//  if not released, keep taking pictures, will not have enough memory          Camerabitmap = bitmapfactory.decodestream (Cr.openinputstream (MUri));         if (bm != null) {             img.setimagebitmap (BM);        }    }  catch  (filenotfoundexception e)  {         //  Todo auto-generated catch&nbSP;BLOCK        LOG.E ("error",  "Get picture from camera failed =====");         e.printstacktrace ();     }}


Two ways Android gets pictures from camera

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.