In the project when we upload pictures, often not choose a good image directly upload, but need to do some operations, such as cutting. Usually my tailoring is like this.
Public voidStartphotozoom (Uri Uri,intWidthintheight) { //crop a pictureIntent Intent =NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI,"Image/*"); //The following crop=true is set to set the display in the open intent view can be croppedIntent.putextra ("Crop", "true"); //Aspectx Aspecty is the ratio of width to height//the wide height (proportional aspectx aspecty) cut in the pass if(Android.os.Build.MODEL.contains ("HUAWEI")) { //Huawei Special Processing Otherwise it will show the circle if(Width! = 0 && Height! = 0) {Intent.putextra ("Aspectx", 9998); Intent.putextra ("Aspecty", 9999*height/width); } Else{Intent.putextra ("Aspectx", 9998); Intent.putextra ("Aspecty", 9999); } } Else { if(Width! = 0 && Height! = 0) {Intent.putextra ("Aspectx", width); Intent.putextra ("Aspecty", height); } Else{Intent.putextra ("Aspectx", 1); Intent.putextra ("Aspecty", 1); } } //Outputx Outputy is the cropped picture width high//widht/height = 500/x; if(Width! = 0 && Height! = 0) {Intent.putextra ("Outputx", 500); Intent.putextra ("Outputy", 500*height/width); }Else{Intent.putextra ("Outputx", 300); Intent.putextra ("Outputy", 300); } Intent.putextra ("Return-data",true); Activity.startactivityforresult (Intent, staticinapp.zoom_image); }
Then we'll call in the Onactivityforresult.
Startphotozoom (Uriutils.pathtouri (This, Selectpath), 1, 1)
The image clipping function is then adjusted. After cropping is finished, the result of cropping is returned under the Staticinapp.zoom_image tab.
However, there is a problem with this approach, in some phones such as Huawei, when the size is larger than 300*300, our phone will crash. This has made us very nervous. This has a pit, that is, when we need a clearer picture, the size of this 300*300 is often not enough to meet our needs. So why is this kind of problem happening? The original in the Huawei and other mobile phones have a problem, the cropped image due to the sharpness is too high, the picture is too large, resulting in the way through Intent.putextra ("Return-data", true); So we changed a method to cut the image.
We first define a global variable private Uri Imagecropuri;
We then write a jump method in the current class and a way to delete the current picture.
/*** Huawei and other mobile phone large-capacity cutting anti-memory overflow *@paramURI *@paramWidth *@paramHeight*/ Public voidCroprawphoto (Uri Uri,intWidthintheight) {Intent Intent=NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI,"Image/*"); Intent.putextra ("Crop", "true"); if(Android.os.Build.MODEL.contains ("HUAWEI") {Intent.putextra ("Aspectx", 9998); Intent.putextra ("Aspecty", 9999 * Height/width); } Else{Intent.putextra ("Aspectx", 1); Intent.putextra ("Aspecty", 1 * Height/width); } Intent.putextra ("Outputx", 700); Intent.putextra ("Outputy", 700); String Path= "file://" + "/" + environment.getexternalstoragedirectory (). GetPath () + "/yulin/" + "small.jpg"; File File=NewFile (path); Delfile (file); Imagecropuri=uri.parse (path); Intent.putextra (Mediastore.extra_output, Imagecropuri); Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection",true); Intent.putextra ("Return-data",false); Startactivityforresult (Intent, staticinapp.zoom_image); } /*** Delete temporary files * *@return */ Private voiddelfile (file file) {if(File.exists ()) {file.delete (); } }
As you can see, the idea is to save the picture in a current file directory, and then do the next step.
Also in the staticinapp.zoom_image in Onactivityforresult, we do the following
if NULL { try { = Bitmapfactory.decodestream (Getcontentresolver ()). Openinputstream (Imagecropuri)); Catch (FileNotFoundException e) { e.printstacktrace (); } Else { LOG.E ("FFL", "Onactivityresult:-------------intent is null------------"); }
We can get to the desired picture.
About the big picture clipping in Huawei and other mobile phones can not use the problem