Android Photography and photo upload implementation code

Source: Internet
Author: User
The code is as follows Copy Code
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;


Import 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;


Private Button capture;


Private Button recapture;


Private Onclicklistener Imgviewlistener;


Private Bitmap Mybitmap;


Private byte[] mcontent;


Private Handler 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 (savedinstancestate);


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 ();


/**


* Because of the Startactivityforresult method used in both ways,


* This method executes the Onactivityresult method after execution, so in order to distinguish between the chosen way to get the picture to be judged,


* The Requestcode here corresponds to the second parameter in the Startactivityforresult.


*/


if (Requestcode = 0)


{


Try


{


Get the URI of a picture


Uri Originaluri = Data.getdata ();


Parse a picture's contents into a byte array


Mcontent = Readstream (Resolver.openinputstream (Uri.parse (originaluri.tostring ())));


Converts a byte array to a ImageView callable bitmap object


Mybitmap = getpicfrombytes (mcontent, NULL);


Bind the resulting picture to display on the control


Preview.setimagebitmap (MYBITMAP);


catch (Exception 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, BAOs);


Mcontent = Baos.tobytearray ();


catch (Exception e)


{


E.printstacktrace ();


}


Bind the resulting picture to display on the control


Preview.setimagebitmap (Imageutil.toroundcorner (Mybitmap, 10))//Turn the shot to a rounded corner displayed 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 =


{"Photo album", "Photo"};


Alertdialog dlg = new Alertdialog.builder (cameraactivity.this). Settitle ("select Picture"). Setitems (Items,


New Dialoginterface.onclicklistener ()


{


public void OnClick (dialoginterface dialog, int item)


{


Here the item is based on the chosen way,


Two ways are defined in the items array, and the camera is labeled 1 so the camera method is called.


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 =


{"Photo album", "Photo"};


Alertdialog dlg = new Alertdialog.builder (cameraactivity.this). Settitle ("select Picture"). Setitems (Items,


New Dialoginterface.onclicklistener ()


{


public void OnClick (dialoginterface dialog, int item)


{


Here the item is based on the chosen way,


Two ways are defined in the items array, and the camera is labeled 1 so the camera method is called.


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 ();//Open Thread submit data


}


Break


Case r.id.back:{


CameraActivity.this.finish ()//return, destroy 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.