Use BitmapFactory to obtain images and built-in image cropping tools suitable for the screen size
Package com. lin. image;
Import android. app. Activity;
Import android. content. Intent;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. ImageView;
Public class ImageScaleActivity extends Activity implements View. OnClickListener {
/** Called when the activity is first created .*/
Private Button selectImageBtn;
Private Button cutImageBtn;
Private ImageView imageView;
Private static final int IMAGE_SELECT = 1;
Private static final int IMAGE_CUT = 2;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
SelectImageBtn = (Button) findViewById (R. id. selectImageBtn );
CutImageBtn = (Button) findViewById (R. id. Adjust magebtn );
ImageView = (ImageView) findViewById (R. id. imageView );
CutImageBtn. setOnClickListener (this );
SelectImageBtn. setOnClickListener (this );
}
@ Override
Public void onClick (View v ){
<Span style = "white-space: pre"> </span> // capture an image of the right size.
If (v = selectImageBtn ){
Intent intent = new Intent (Intent. ACTION_PICK,
Android. provider. MediaStore. Images. Media. EXTERNAL_CONTENT_URI );
StartActivityForResult (intent, IMAGE_SELECT );
} // Generally, this parameter is used to set the size of an avatar.
Else if (v = cutImageBtn ){
Intent intent = getimag1_intent ();
StartActivityForResult (intent, IMAGE_CUT );
}
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent intent ){
// TODO Auto-generated method stub
If (resultCode = RESULT_ OK ){
If (requestCode = IMAGE_SELECT ){
Uri imageFileUri = intent. getData ();
Int dw = getWindowManager (). getdefadefadisplay (). getWidth ();
Int dh = getWindowManager (). getdefadisplay display (). getHeight ()/2;
// The Screen width and general height are used as the maximum size for image display.
Try {
BitmapFactory. Options factory = new BitmapFactory. Options ();
Factory. inJustDecodeBounds = true; // when the value is true, the system allows you to query images without allocating memory for image pixels.
Bitmap bmp = BitmapFactory. decodeStream (getContentResolver ()
. OpenInputStream (imageFileUri), null, factory );
Int hRatio = (int) Math. ceil (factory. outHeight/(float) dh); // The image is several times the height.
Int wRatio = (int) Math. ceil (factory. outWidth/(float) dw); // The image is several times the width.
System. out. println ("hRatio:" + hRatio + "wRatio:" + wRatio );
// Scale down to 1/ratio and 1/ratio ^ 2 pixels
If (hRatio> 1 | wRatio> 1 ){
If (hRatio> wRatio ){
Factory. inSampleSize = hRatio;
}
Else
Factory. 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 );
}
}
}
/**
* Obtain the cut image.
*/
Public static Intent getImageClipIntent (){
Intent intent = new Intent (Intent. ACTION_GET_CONTENT, null );
Intent. setType ("image /*");
Intent. putExtra ("crop", "true ");
Intent. putExtra ("aspectX", 1); // ratio of the cropping Frame
Intent. putExtra ("aspectY", 1 );
Intent. putExtra ("outputX", 80); // output image size
Intent. putExtra ("outputY", 80 );
Intent. putExtra ("return-data", true );
Return intent;
}
}
From the column dikeboy1234