Android how to take pictures and return pictures (verified examples)

Source: Internet
Author: User
Package. test; import Java. io. bytearrayoutputstream; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. inputstream; import android. app. activity; import android. content. activitynotfoundexception; import android. content. contentresolver; import android. content. intent; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android.net. Uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediastore; import android. view. gravity; import android. widget. imageview; import android. widget. toast; public class A extends activity {/** called when the activity is first created. */string sd_card_temp_dir; private byte [] mcontent; bitmap mybitmap; @ override public void oncreate (bundle savedinstancestate) {super. O Ncreate (savedinstancestate); setcontentview (R. layout. main); // sd_card_temp_dir = environment. getexternalstoragedirectory () + file. separator + "tmpphoto.jpg"; intent takepicturefromcameraintent = new intent (mediastore. action_image_capture); takepicturefromcameraintent. putextra (Android. provider. mediastore. extra_output, Uri. fromfile (new file (sd_card_temp_dir); startactivityforresult (takepicture Fromcameraintent, 10); //} // @ override protected void onactivityresult (INT requestcode, int resultcode, intent data) {contentresolver resolver = getcontentresolver (); try {file F = new file (sd_card_temp_dir); try {URI capturedimage = Uri. parse (Android. provider. mediastore. images. media. insertimage (getcontentresolver (), F. getabsolutepath (), null, null); // log. I ("camera", "selected image :" + // Capturedimage. tostring (); // F. delete (); Toast = toast. maketext (getapplicationcontext (), capturedimage. tostring (), toast. length_short); toast. setgravity (gravity. center_horizontal, 0, 0); toast. show (); // imageview A = (imageview) findviewbyid (R. id. imageview1); // parse the image content into a byte array mcontent = readstream (resolver. openinputstream (URI. parse (capturedimage. tostring (); // converts a byte array to an image View Bitmap object mybitmap = getpicfrombytes (mcontent, null); // binds the obtained image to the control and displays. setimagebitmap (mybitmap); //} catch (filenotfoundexception e) {// todo auto-generated Catch Block E. printstacktrace () ;}} catch (exception e) {// todo: handle exception system. out. println (E. getmessage () ;}}// public static bitmap getpicfrombytes (byte [] bytes, bitmapfactory. options opts) {If (Bytes! = NULL) if (OPTs! = NULL) return bitmapfactory. decodebytearray (bytes, 0, bytes. length, opts); else return bitmapfactory. decodebytearray (bytes, 0, bytes. length); return NULL;} // public static byte [] readstream (inputstream instream) throws exception {byte [] buffer = new byte [1024]; int Len =-1; bytearrayoutputstream outstream = new bytearrayoutputstream (); While (LEN = instream. read (buffer ))! =-1) {outstream. write (buffer, 0, Len);} byte [] DATA = outstream. tobytearray (); outstream. close (); instream. close (); return data;} // public static bitmap getloacalbitmap (string URL) {try {fileinputstream FD = new fileinputstream (URL); Return bitmapfactory. decodestream (FS);} catch (filenotfoundexception e) {e. printstacktrace (); return NULL ;}}//}

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.