Android how to take pictures and return pictures (verified examples)
Last Update:2018-12-07
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 ;}}//}