Android Select picture to take pictures and cut photos to upload to server

Source: Internet
Author: User

Interface Call method: Pickerpicture and Pickerpictureok as the parent class method, the parent class method calls the upload, the interface only needs to handle these two methods;



Handle open photos in parent class, select Finish and upload to server call upload completion notification interface




Select picture and cut, 4.4 of the phone is different from other

Package Com.idonoo.sharecar.ui.commom.activitys;import Java.io.file;import Android.app.activity;import Android.content.intent;import Android.graphics.bitmap;import Android.os.asynctask;import Android.os.Bundle;import Android.text.textutils;import Android.view.view;import Android.widget.textview;import Com.idonoo.frame.model.imagestorage;import Com.idonoo.frame.utils.imagestorehelper;import Com.idonoo.frame.utils.imageutil;import Com.idonoo.sharecar.r;import Com.idonoo.sharecar.app.intentextra;import Com.idonoo.sharecar.app.intentresult;import Com.idonoo.sharecar.uiframe.baseactivity;import Com.idonoo.sharecar.utils.photoutility;import Com.intbird.soft.utils.bitmaphelper;import Com.intbird.soft.utils.cachehelper;import Com.intbird.soft.utils.phonehelper;public class TakeImageActivity Extends baseactivity {private int viewid;imagestorehelper.imageoptions imageoptions;private imagestorage imageFile; private string Orignalfileurl= "";p rivate string cutedfileurl= "";p rivate string TempnewfilenAme= "";p rivate copyimagetask task; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.layout_popup_take_image); Initui (); InitData (); Cachehelper.initcache ("Idonoo");//actually only need once, if I used when need it;} @Overrideprotected void Initui () {Super.initui (); Super.initactionbar (); Setviewclicklistener (Listener,r.id.btn_ Select,r.id.btn_take_photo,r.id.cancel);} @Overrideprotected void InitData () {viewid=getintent (). Getintextra (intentextra.extra_imagesotre_view_id, 0); Imagefile= (Imagestorage) getintent (). Getserializableextra (intentextra.extra_imagesotre_store_file); o Rignalfileurl=imagefile.getfilepath (); Imageoptions=imagefile.getoptions (); Setsheetview ();} private void Setsheetview () {int sheettext=getintent (). Getintextra (Intentextra.extra_imagesotre_sheet_text, 0); if ( sheettext!=0) {TextView Tvtip = (TextView) Findviewbyid (R.id.tv_tip_text); Tvtip.settext (GetString (Sheettext)); Tvtip.setvisibility (view.visible);}} @Overrideprotected void Onsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate); Outstate.putstring (" Orignalfileurl ", Orignalfileurl); Outstate.putstring (" Cutedfileurl ", Cutedfileurl); Outstate.putstring (" Tempnewfilename ", tempnewfilename);} @Overrideprotected void Onrestoreinstancestate (Bundle savedinstancestate) {super.onrestoreinstancestate ( Savedinstancestate); Orignalfileurl=savedinstancestate.getstring ("Orignalfileurl"); cutedFileUrl= Savedinstancestate.getstring ("Cutedfileurl"); Tempnewfilename=savedinstancestate.getstring ("TempNewFilename");} View.onclicklistener listener = new View.onclicklistener () {@Overridepublic void OnClick (View v) {switch (V.getid ()) {CAs E r.id.btn_select: {Try{take ();} catch (Exception ex) {Showtoast ("Turn on the phone to take a photo failed \ n Try to select from the album ~");}} Break;case R.id.btn_take_photo: {try{showtoast ("Please select a photo from the system gallery)" Select (); catch (Exception ex) {showtoast ("Choose from album failed \ n please use other selection software Oh ~");}} Break;case r.id.cancel: {finish (false);} break;}}; private void Take () {Tempnewfilename=imagefile.getfilenaMe () +system.currenttimemillis () + ". jpg"; Orignalfileurl=cachehelper.createcamerafile (Tempnewfilename). GetAbsolutePath (); Intent Intent = Photoutility.gotocamera (Orignalfileurl); Startactivityforresult (Intent, Intentresult.rs_camera);} private void Select () {Intent Intent = Photoutility.gotogallery (); if (Phonehelper.isversionhight ()) {intent= Photoutility.gotogallerypick ();} Startactivityforresult (Intent, intentresult.rs_gallery);} private void Crop () {tempnewfilename=imagefile.getfilename () +system.currenttimemillis () + ". jpg"; cutedfileurl= Cachehelper.createimagefile (Tempnewfilename). GetAbsolutePath (); Try{intent Intent = Photoutility.cropimagefile ( Orignalfileurl,cutedfileurl,imageoptions.cropwidth,imageoptions.cropheight); Startactivityforresult (Intent, Intentresult.rs_crop);} catch (Exception ex) {showtoast ("Missing picture clipping program in Phone \ n will compress upload original image, please ensure the picture size is appropriate"); cutedfileurl=orignalfileurl;task=new Copyimagetask (imageoptions); Task.execute ();}} private void Cropok () {task=new copyimagetask (imageoptions); Task.execute();} @Overridepublic void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = = Activity.result_ok {switch (requestcode) {case Intentresult.rs_camera: {crop ();} Break;case intentresult.rs_gallery: {String tempgeturl= ""; Try{tempgeturl = Imageutil.getpathfromuri (Data.getData ()) ;} catch (Exception ex) {tempgeturl= ""; Showtoast ("A bit of a problem with the seemingly selected picture. \ n Please re-select the picture"); if (Textutils.isempty (Tempgeturl)) {Showtoast ("There is something wrong with the seemingly selected picture \ n Please re-select the picture"); return;} Orignalfileurl = Tempgeturl;crop ();} Break;case Intentresult.rs_crop: {Cropok ();} Break;}}} Private class Copyimagetask extends Asynctask<void, Void, file>{private imagestorehelper.imageoptions options; Public Copyimagetask (Imagestorehelper.imageoptions imageoptions) {this.options=imageoptions;showprogress ("compressing picture" );} @Overrideprotected File doinbackground (Void ... params) {bitmapcroped = Bitmaphelper.getbitmapfromfile (Cutedfileurl, Options.cropwidth,options.cropheight)///If the image size after the cut is empty, indicating that the cut failed, try to get the original if (croped==null) {croped = bitmaphelper.geTbitmapfromfile (Orignalfileurl, options.cropwidth,options.cropheight);} If the original capture fails, return the original address to file Newfile;if (croped==null) {newfile=new file (orignalfileurl); return newFile;} Bitmap Bitmap = Bitmaphelper.compressbitmap (croped, options.maxkb); newFile = Cachehelper.copybitmaptocache (Bitmap, Tempnewfilename); return newFile;} @Overrideprotected void OnPostExecute (file file) {super.onpostexecute (file);d ismissprogress (); if (file==null| |! File.exists ()) {showtoast ("Copy selected file to Idonoo folder failed!\n Check if image or memory card is available ~"); return; if (File.length () <10*1024) {file.delete (); Showtoast ("Upload image less than 10KB, please re-capture the big picture ~"); return; Imagefile.setfilepath (File.getabsolutepath ()); Finish (true);}} private void Finish (Boolean isOk) {if (!isok) viewid=0;intent intent=new Intent (); Intent.putextra (Intentextra.extra_ imagesotre_view_id, viewId); Intent.putextra (Intentextra.extra_imagesotre_store_file, ImageFile); SetResult ( Activity.result_ok,intent); if (task!=null&&!task.iscancelled ()) Task.cancel (true); Finish ();}}

Package Com.idonoo.sharecar.utils;import Android.content.intent;import Android.graphics.bitmap;import Android.net.uri;import Android.provider.mediastore;import Java.io.file;public class PhotoUtility {public static Inten T Gotogallery () throws nullpointerexception{Intent Intent = new Intent (intent.action_get_content, null);//action_        Pick//action_get_content Intent.setdata (MediaStore.Images.Media.EXTERNAL_CONTENT_URI);        Intent.settype ("image/*");    return intent; } public static Intent Gotogallerypick () throws nullpointerexception{Intent Intent = new Intent (intent.acti On_pick, NULL);        Action_pick//action_get_content Intent.setdata (MediaStore.Images.Media.EXTERNAL_CONTENT_URI);        Intent.settype ("image/*");    return intent; } public static Intent Gotocamera (String filePath) throws nullpointerexception{Intent Intent = new Intent (Media        Store.action_image_capture); Intent.putextra (Mediastore.extra_output, Uri. FromFile (New File (FilePath)));    return intent; } public static Intent Cropimage (Uri Uri, String filePath, int width, int height) {Intent Intent = new Intent ("        Com.android.camera.action.CROP ");        Intent.setdataandtype (URI, "image/*");        Intent.putextra ("Crop", "true");        Intent.putextra ("Aspectx", width);        Intent.putextra ("aspecty", height);        Intent.putextra ("Outputx", width);        Intent.putextra ("outputy", height);        Intent.putextra ("Return-data", false);        Intent.putextra ("Nofacedetection", true);         Intent.putextra ("scale", true);         Intent.putextra ("scaleupifneeded", true);        Intent.putextra (Mediastore.extra_output, Uri.fromfile (New File (FilePath)));    return intent; } public static Intent cropimage (String filePath, int width, int height) {Uri uri=uri.fromfile (new File (fil        Epath));        Intent Intent = new Intent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI, "image/* ");        Intent.putextra ("Crop", "true");        Intent.putextra ("Aspectx", width);        Intent.putextra ("aspecty", height);        Intent.putextra ("Outputx", width);        Intent.putextra ("outputy", height);        Intent.putextra ("Return-data", false);        Intent.putextra ("Nofacedetection", true);         Intent.putextra ("scale", true);         Intent.putextra ("scaleupifneeded", true);        Intent.putextra (Mediastore.extra_output,uri);    return intent;  } public static Intent Cropimagefile (String oldpath,string filepath,int width, int height) {Intent Intent =            New Intent ("Com.android.camera.action.CROP");        Intent.setdataandtype (Uri.fromfile (New File (OldPath)), "image/*");        Intent.putextra ("Crop", "true");        Intent.putextra ("Aspectx", width);        Intent.putextra ("aspecty", height);        Intent.putextra ("Outputx", width);        Intent.putextra ("outputy", height); Intent.putextra ("OutputFormat", BITMAP.COMPRESSFORMAT.JPeg.tostring ());        Intent.putextra ("Return-data", false);        Intent.putextra ("Nofacedetection", true);         Intent.putextra ("scale", true);         Intent.putextra ("scaleupifneeded", true);        Intent.putextra (Mediastore.extra_output,uri.fromfile (New File (FilePath)));    return intent; }}



Android Select picture to take pictures and cut photos to upload to server

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.