Package com.example.uploadhead; Import Java.io.ByteArrayInputStream; Import Java.io.ByteArrayOutputStream; Import Java.io.File; Import Java.io.InputStream; Import Java.text.SimpleDateFormat; Import Java.util.Date; Import Android.net.Uri; Import Android.os.Bundle; Import android.os.Environment; Import Android.provider.MediaStore; Import android.app.Activity; Import android.content.Intent; Import Android.graphics.Bitmap; Import Android.graphics.BitmapFactory; Import Android.view.View; Import Android.widget.Button; Import Android.widget.ImageView; public class Mainactivity extends Activity { public static final int NONE = 0; public static final int photo_camera = 1;//camera photo public static final int photo_compile = 2; Edit Picture public static final int photo_resoult = 3;//result ImageView Headimageview = null; Button Photo_button = null; Button Camera_button = null; Private String ImageName; public static String Getstringtoday () { Date currenttime = new Date (); SimpleDateFormat formatter = new SimpleDateFormat ("Yyyymmddhhmmss"); String datestring = Formatter.format (currenttime); return datestring; }
@Override public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);
Headimageview = (ImageView) Findviewbyid (R.id.imageid);//Find the ImageView to fill
Photo_button = (Button) Findviewbyid (r.id.btn_01);//album Button Camera_button = (Button) Findviewbyid (r.id.btn_02);//Camera button
Open the system's album Photo_button.setonclicklistener (New View.onclicklistener () { public void OnClick (View v) { Set the intention to invoke the System album (implicit Intent) Intent Intent = new Intent ();
Set Value activity//android.intent.action.pick Intent.setaction (Intent.action_pick);
Set type and data Intent.setdataandtype ( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
Open the system's album Startactivityforresult (Intent, photo_compile); } });
Turn on the system camera Camera_button.setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) { Set the name of the picture ImageName = "/" + getstringtoday () + ". jpg"; Set the intent to invoke the system camera (implicit intent) Intent Intent = new Intent (mediastore.action_image_capture);
Set the output path and file name of the photo
File file= new file (Environment.getexternalstoragedirectory (), ImageName);
Intent.putextra (Mediastore.extra_output, Uri.fromfile (file)); Turn on the camera Startactivityforresult (Intent, Photo_camera); } }); } Call Startactivityresult, return the callback function after @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) { if (ResultCode = = NONE) Return
Picture taking pictures by camera if (Requestcode = = Photo_camera) { Set file save path place it in the directory below. File picture = new file (Environment.getexternalstoragedirectory () + ImageName); Crop a picture Startphotozoom (Uri.fromfile (picture)); } if (data = = NULL) Return Reading album cropping pictures if (Requestcode = = Photo_compile) { Crop a picture Startphotozoom (Data.getdata ()); }
Crop photo processing results if (Requestcode = = Photo_resoult) {
Bundle extras = Data.getextras ();
if (extras! = null) {
Bitmap photo = extras.getparcelable ("Data");
Bytearrayoutputstream stream = new Bytearrayoutputstream (); Photo.compress (Bitmap.CompressFormat.JPEG, (), stream);//(0- 100) Compress files
Set up picture display content Headimageview.setimagebitmap (photo); } } Super.onactivityresult (Requestcode, ResultCode, data); }
/** to call the cropping picture function of the system * * @param the path of the URI picture */ public void Startphotozoom (Uri uri) {
Intent Intent = new Intent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI, "image/*"); Intent.putextra ("Crop", "true"); Aspectx Aspecty is the ratio of width to height Intent.putextra ("Aspectx", 1); Intent.putextra ("Aspecty", 1); Outputx Outputy is the cropped picture width high Intent.putextra ("Outputx", 64); Intent.putextra ("Outputy", 64); Intent.putextra ("Return-data", true);
Startactivityforresult (Intent, Photo_resoult); } } |