Preface--The project needs to use the user picture of the cutting and uploading function. About cutting, the beginning is to want to do their own, but think this thing should Google has developed it, so a search official documents, sure enough. As a result, the decisive shameless use of Android from the free clipping of photos. Because the time is too tight, although not too gorgeous, but wins in can use, save time.
specifically, through Intent of the Acti on to achieve.
The key code is as follows:
public void Imagecut (Uri uri) {Intent Intent = new Intent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI, "image/*");//Turn on the cropping function Intent.putextra ("Crop", "true");//Set the ratio of the wide Intent.putextra ("Aspectx", 1); Intent.putextra (" Aspecty ", 1);//Set crop image width height intent.putextra (" Outputx "," X "), Intent.putextra (" Outputy ", 100);//request to return data Intent.putextra (" Return-data ", true); Startactivityforresult (intent, 100);}
returns data directly after the Onactivityresult in the return of the image data to display on the line, here because of the cut after the picture is small, the personal feel should not consider The problem of OOM, the problem is, when the call comes with the cutting function, because did not see the source code, So do not know when adjusting the cropping box, the display of the picture has not been compressed, if not compression, then when the picture is very large, it is easy to load up OOM , obviously because it is called the official API , I have no way to solve it. The only way is not to use it, but to write one yourself, you can avoid this problem. Use the following code to compress, to avoid the OOM problem well.
/* * Compress picture, return compressed photo */public static Bitmap revitionimage (String path,int size) {Bitmap Bitmap = null;try {///first open picture file with picture path, slow Rushed to a cache input stream bufferedinputstream in = new Bufferedinputstream (new FileInputStream (path));//Use parameters to represent variables to record information about the current photo, Size Bitmapfactory.options Options = new Bitmapfactory.options ();//Set Injustdecodebounds to True, You can get the information to the bitmap file bitmap without allocating memory at all,//so that it is size, it can be 7 compressed, and then generate a smaller bitmap in memory, saving memory Options.injustdecodebounds = true;// Decode the bitmap file only in order to get the original information Bitmapfactory.decodestream (in,null,options);//Remember to close the stream in.close (); int i = 0;while (true) {// The right shift I bit is because the following POW takes half, the newly generated picture is the original One-second if (((Options.outwidth >> i) <=size) && ((Options.outheight >> i) (<=size)) {//This time should take the photograph in = new Bufferedinputstream (new FileInputStream (path));//pow is the second-party method, 2 I-square, Insamplesize picture evenly sampled values, such as//insamplesize = = 4 Returns an image that's the width/height of the original, and 1/16 the numb Er of pixels. options.insamplesize = (int) Math.pow (2.0, I);//This time the picture is already in line with our set size of 256, so to set false, allow output, create image//To really generate a pixel-scaled bitmapoptions.injustdecodebounds = False;bitmap = Bitmapfactory.decodestream (in,null,options);// Get one of the original n-points, less than 256*256break;} else {i++;}}} catch (IOException e) {log.i (TAG, "Compressed picture error" +e.tostring ());} return bitmap;}
Finally can only use the first, have time to dig deeper or oneself toss a. Here it is, looking at the line.
Here is a demo on this topic. Children's shoes in need can be seen.
Demo sample Download