Use Bitmapfactory to get a screen-sized picture and your own picture cropping tool
PackageCom.lin.image; Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView; Public classImagescaleactivityextendsActivityImplementsview.onclicklistener{/**Called when the activity is first created.*/ PrivateButton selectimagebtn; PrivateButton cutimagebtn; PrivateImageView ImageView; Private Static Final intImage_select=1; Private Static Final intimage_cut=2; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); SELECTIMAGEBTN=(Button) Findviewbyid (R.ID.SELECTIMAGEBTN); CUTIMAGEBTN=(Button) Findviewbyid (R.ID.CATIMAGEBTN); ImageView=(ImageView) Findviewbyid (R.id.imageview); Cutimagebtn.setonclicklistener ( This); Selectimagebtn.setonclicklistener ( This); } @Override Public voidOnClick (View v) {<span style= "White-space:pre" > </span>//capture images that fit the screen size if(v==selectimagebtn) {Intent Intent=NewIntent (Intent.action_pick, Android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); Startactivityforresult (Intent, image_select); }//typically used for avatars such as the need to set the specified size Else if(v==cutimagebtn) {Intent Intent=getimageclipintent (); Startactivityforresult (Intent, image_cut); }} @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent Intent) { //TODO auto-generated Method Stub if(resultcode==RESULT_OK) { if(requestcode==image_select) {Uri Imagefileuri=Intent.getdata (); intdw=Getwindowmanager (). Getdefaultdisplay (). GetWidth (); intDh=getwindowmanager (). Getdefaultdisplay (). GetHeight ()/2; //screen width and general height as the maximum size of the picture display Try{bitmapfactory.options Factory=Newbitmapfactory.options (); Factory.injustdecodebounds=true;//allow query pictures to not allocate memory for picture pixels when TrueBitmap bmp=Bitmapfactory.decodestream (Getcontentresolver (). Openinputstream (Imagefileuri),NULL, Factory); intHratio= (int) Math.ceil (factory.outheight/(float) DH);//the picture is several times the height intWratio= (int) Math.ceil (factory.outwidth/(float) (DW);//the picture is several times the widthSystem.out.println ("Hratio:" +hratio+ "Wratio:" +wratio); //reduced to 1/ratio size and 1/ratio^2 pixels if(hratio>1| | Wratio>1){ if(hratio>wratio) {Factory.insamplesize=Hratio; } Elsefactory.insamplesize=Wratio; } factory.injustdecodebounds=false; BMP=Bitmapfactory.decodestream (Getcontentresolver (). Openinputstream (Imagefileuri),NULL, Factory); Imageview.setimagebitmap (BMP); }Catch(Exception ex) {}}Else if(requestcode==image_cut) {Bitmap BMP=intent.getparcelableextra ("Data"); Imageview.setimagebitmap (BMP); } } } /*** Get the picture after cut*/ Public StaticIntent getimageclipintent () {Intent Intent=NewIntent (Intent.action_get_content,NULL); Intent.settype ("Image/*"); Intent.putextra ("Crop", "true"); Intent.putextra ("Aspectx", 1);//Crop Frame ScaleIntent.putextra ("Aspecty", 1); Intent.putextra ("Outputx", 80);//Output picture sizeIntent.putextra ("Outputy", 80); Intent.putextra ("Return-data",true); returnIntent; } }
Excerpt from Dikeboy1234 's column
Android crop picture size control picture size