Android photography and photo upload

Source: Internet
Author: User
Package COM. android. cist. camera. view; import Java. io. bytearrayoutputstream; import Java. io. inputstream; import Java. util. hashmap; import Java. util. map; import android. app. activity; import android. app. alertdialog; import android. content. contentresolver; import android. content. dialoginterface; import android. content. intent; import android. graphics. bitmap; import android. graphics. bitmapfactory; import Android.net. uri; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. widget. imageview; import android. widget. linearlayout; import android. widget. toast; import COM. android. cist. r; import COM. android. cist. network. httputil; impor T COM. android. cist. network. form. formfile; import COM. android. cist. network. form. httpformutil; import COM. android. cist. util. applicationutil; import COM. android. cist. util. imageutil; public class cameraactivity extends activity implements onclicklistener {private imageview preview; private edittext carno; private edittext addres; private edittext type; private button back; private button submit; priva Te button capture; private button recapture; private onclicklistener imgviewlistener; private bitmap mybitmap; private byte [] mcontent; private handler; string returnstring; private calendarview happendate; private Static final int request_camera = 1; Private Static final int request_calendar = 2; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstances Tate); setcontentview (R. layout. camera); setupviews (); setuplisteners (); applicationutil. getinstance (). addactivity (this) ;}@ override protected void onactivityresult (INT requestcode, int resultcode, intent data) {super. onactivityresult (requestcode, resultcode, data); contentresolver resolver = getcontentresolver ();/*** the startactivityforresult method is used in both methods. * The onactivityresult method is executed after the method is executed, institute The difference is determined by the method used to obtain the image. * The requestcode here corresponds to the second parameter in startactivityforresult */If (requestcode = 0) {try {// retrieve the image URI originaluri = data. getdata (); // parse the image content into a byte array mcontent = readstream (resolver. openinputstream (URI. parse (originaluri. tostring (); // convert the byte array to the bitmap object mybitmap = getpicfrombytes (mcontent, null) that can be called by imageview ); //// bind the obtained image to the control to display the preview. setimagebitmap (mybitmap);} catch (Response t Ion e) {system. out. println (E. getmessage () ;}} else if (requestcode = request_camera) {try {super. onactivityresult (requestcode, resultcode, data); bundle extras = data. getextras (); mybitmap = (Bitmap) extras. get ("data"); bytearrayoutputstream baos = new bytearrayoutputstream (); mybitmap. compress (bitmap. compressformat. JPEG, 100, baos); mcontent = baos. tobytearray ();} catch (exception e) {e. Printstacktrace ();} // bind the obtained image to the control to display the preview. setimagebitmap (imageutil. toroundcorner (mybitmap, 10); // convert the taken photo to a rounded corner and display it on the preview control} else if (requestcode = request_calendar) {If (resultcode = result_ OK) {happendate. setcalendar (data. getintextra ("year", 1900), Data. getintextra ("month", 0), Data. getintextra ("day", 1) ;}} 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 ;}@ override public void onclick (view v) {int id = v. GETID (); Switch (ID) {case R. id. capture: {final charsequence [] items = {"album", "photo"}; alertdialog DLG = new alertdialog. builder (cameraactivity. this ). settitle ("select image "). setitems (items, new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int item) {// here the item is selected based on the method, // two methods are defined in the items array, the subscript of the image is 1, so you can call the photo method if (item = 1) {intent getimagebycamera = new intent ("android. media. action. image_capture "); startactivityforresult (getimagebycamera, request_camera);} else {intent getimage = new intent (intent. action_get_content); getimage. addcategory (intent. category_openable); getimage. settype ("image/JPEG"); startactivityforresult (getimage, 0 );}}}). create (); DLG. show ();} break;/* case R. id. recapture: {final charsequence [] items = {"album", "photo"}; alertdialog DLG = new alertdialog. builder (cameraactivity. this ). settitle ("select image "). setitems (items, new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int item) {// here the item is selected based on the method, // two methods are defined in the items array, the subscript of the image is 1, so you can call the photo method if (item = 1) {intent getimagebycamera = new intent ("android. media. action. image_capture "); startactivityforresult (getimagebycamera, 1);} else {intent getimage = new intent (intent. action_get_content); getimage. addcategory (intent. category_openable); getimage. settype ("image/JPEG"); startactivityforresult (getimage, 0 );}}}). create (); DLG. show ();} break; */case R. id. submit: {New postthread (). start (); // enable the thread to submit data} break; case R. id. back: {cameraactivity. this. finish (); // return, destroy the current activity} break; default: Break ;}}}
Related Article

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.