First, use.
1. Start chart selection (camera, photo album)
This code starts the camera photo Selectpicutil.getbycamera (this);//This code jumps to the photo album Selectpicutil.getbyalbum (this);
2, processing pictures, processing in the Onactivityresult
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {//Output picture 800*400 size, clip ratio when selecting picture is 2:1 BM = Sel Ectpicutil.onactivityresult (This, Requestcode, ResultCode, data, 800, 400, 2, 1); if (Bm!=null) {...}}
OK, the usage is so simple! At present I test all the models are no problem (of course not all models), the picture does not have to consider the size, anyway, it is stored SD card, will not be memory overflow. However, note the need to read and write the SD card permissions (the general project will certainly be).
Then there's the code.
← ←copy time to leave a author can, after all, people write so hard.
Import java.io.filenotfoundexception;import android.app.activity;import android.content.context ;import android.content.intent;import android.graphics.bitmap;import android.graphics.bitmapfactory;import android.net.uri;import android.os.environment;import android.provider.mediastore;/** * Select Local Picture Tool class * <br> * because the direct access to the picture easily crashes, So directly into the SD card, and then get * <br> * because the wrong way to make some models can not be used, so package up and reuse * <br> * How to use: * <br> * 1, call Getbyalbum, getbycamera to get pictures * <br> * &NBSP;2, call the Onactivityresult method of this tool class in Onactivityresult to process * <br> * 3, Onactivityresult return bitmap Hollow pointer to Judge * * <br><br> * ps: This tool class can only handle cropping pictures, If you do not want to crop, do not use the tool class Onactivityresult, do your own processing can * * @author linin630 * */public class selectpicutil { /** temporarily store the address of the picture, if you want to modify, please remember to create the pathUnder the folder */ private static final string lsimg = "File:///sdcard/temp.jpg"; public static final int get_by_album = 801;//If there is a conflict, remember to modify public static final int get_by_camera = 802;//If there is a conflict, remember to modify public static final int crop = 803;//If there is a conflict, remember to modify /** to get pictures from the album */ public static Void getbyalbum (activity act) { intent getalbum = new intent ( intent.action_get_content); getalbum.settype ("image/*"); act.startactivityforresult ( getalbum, get_by_album) } /** get pictures by taking photos */ public static void Getbycamera (activity act) { string state = environment.getexternalstoragestate () ; if (State.equals (environment.media_mounted)) { Intent Getimagebycamera = new intent (mediastOre. Action_image_capture); getimagebycamera.putextra (Mediastore.extra_output, uri.parse ( lsimg)); getimagebycamera.putextra (mediastore.extra_video_quality, 0); Act.startactivityforresult (Getimagebycamera, get_by_camera); } else { &NBSP;L.E ("Make sure you have inserted the SD card"); } } /** * handle the captured picture, note the null pointer, default size 480*480, scale 1:1 */ public static bitmap onactivityresult (activity act, int Requestcode, int resultcode, intent data) { return onactivityresult (Act, &NBSP;REQUESTCODE,&NBSP;RESULTCODE,&NBSP;DATA,&NBSP;0,&NBSP;0,&NBSP;0,&NBSP;0); } /** * Handle the acquired picture, and be careful to judge the null pointer */ public static bitmap onactivityresult (activity Act, int requestcode, int resultcode, intent data, int w, int h,int&nbsP;ASPECTX,INT&NBSP;ASPECTY) { bitmap bm = null; if (resultCode = =&NBSP;ACTIVITY.RESULT_OK) { uri uri = null; switch (Requestcode) { case GET_BY_ALBUM: uri = Data.getdata (); act.startactivityforresult (Crop (uri,w,h,aspectx,aspecty), crop); break; case GET_BY_CAMERA: uri = Uri.parse (lsimg); act.startactivityforresult (Crop (uri,w,h,aspectx,aspecty), crop); break; case crop: bm = dealcrop (ACT); break; } } return bm; } /* * Default crop output 480*480, proportional 1:1*/ public static intent crop (Uri uri) { return Crop (uri,480,480,1,1); } /** * cropping, for example: output 100*100 size picture, width to height ratio is 1:1 * @param w output width * @param h Output high * @param aspectX wide scale * @param aspecty High proportion */ public static intent crop (uri uri,int w,int H,INT&NBSP;ASPECTX,INT&NBSP;ASPECTY) { if (w==0&&h==0) { w=h=480; } if (aspectx==0&&aspecty==0) { aspectX=aspectY=1; } intent intent = new intent ("Com.android.camera.action.CROP"); // Photo URL address intent.setdataandtype (uri, "image/*"); intent.putextra ( "Crop", "true"), intent.putextra ("Aspectx", aspectx); intent.putextra ("AspectY ", aspecty); intent.putextra (" Outputx ", w); intent.putextra (" OutputY ", h) ; //&nBSP; output path intent.putextra (Mediastore.extra_output, uri.parse (lsimg)); // output format intent.putextra ("OutputFormat", "JPEG"); // do not enable face recognition Intent.putextra ("Nofacedetection", true); intent.putextra ("Return-data", false); return intent; } /** handles cropping to get the cropped picture */ public static bitmap Dealcrop (context context) { // cropping returns uri uri = uri.parse (LSIMG); Bitmap bitmap = null; try { bitmap = Bitmapfactory.decodestream (Context.getcontentresolver (). Openinputstream (URI)); } catch ( filenotfoundexception e) { e.printstacktrace (); } return bitmap; }}
If so
If using my tool class problem, trouble to mention here, I will continue to improve the ~ ~ or have any ideas also say Oh ~ ~
Android Select Local picture and Crop tool class