Crop an image from gallery in android

2019-06-05 03:10发布

I want to crop an image in my application when it is selected from gallery.My cropping code work from the simulator but not properly work on phones. I set outputX=400 and outputY =487. In my simulator i get the output bitmap with 400 x 487 resolution,but when i cropped the image from phone gallery i get the output bitmap with 145 x 177 resolution. Why does it happen? My code for cropping is given below

Intent intent = new Intent("com.android.camera.action.CROP");

intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);

intent.putExtra("crop", "true");
intent.putExtra("aspectX", 500);
intent.putExtra("aspectY", 750);
intent.putExtra("scale", true);
intent.putExtra("outputX", 400);
intent.putExtra("outputY", 487);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);

On onActivityResult

if (requestCode == PICK_FROM_GALLERY) {
Bundle extras2 = data.getExtras();
if (extras2 != null) {
Bitmap bm = extras2.getParcelable("data");
imgview.setImageBitmap(photo);}

2条回答
何必那么认真
2楼-- · 2019-06-05 03:47

I think this will address the issue.

http://www.londatiga.net/featured-articles/how-to-select-and-crop-image-on-android/

PS: This code may or may not work on all devices. This bit of code relies on code that is not part of the API. The only way to do cropping is to put the code directly in your app.

查看更多
做个烂人
3楼-- · 2019-06-05 03:50
buttonGallery.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
// call android default gallery
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);

try {

intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_GALLERY);

} catch (ActivityNotFoundException e) {
// Do nothing for now
}
}
});

or Refer these sites for more help:

http://www.londatiga.net/featured-articles/how-to-select-and-crop-image-on-android/ http://www.androidhub4you.com/2012/07/how-to-crop-image-from-camera-and.html

查看更多
登录 后发表回答