【问题标题】:Crop an image from gallery in android从android中的画廊裁剪图像
【发布时间】:2014-02-03 13:18:33
【问题描述】:

当从图库中选择图像时,我想在我的应用程序中裁剪图像。我的裁剪代码可以在模拟器上工作,但不能在手机上正常工作。我设置 outputX=400 和 outputY =487。 在我的模拟器中,我得到了 400 x 487 分辨率的输出位图,但是当我从手机图库中裁剪图像时,我得到了 145 x 177 分辨率的输出位图。为什么会发生?我的裁剪代码如下

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);

onActivityResult

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

【问题讨论】:

标签: android camera android-camera crop resize-crop


【解决方案1】:
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
}
}
});

或参考这些网站以获得更多帮助:

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

【讨论】:

  • 上面写的代码是从相机中裁剪图像。我已经这样做了,我需要从图库中裁剪图像
【解决方案2】:

我认为这将解决问题。

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

PS:此代码可能适用于所有设备,也可能不适用于所有设备。这段代码依赖于不属于 API 的代码。进行裁剪的唯一方法是将代码直接放入您的应用中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 2015-02-02
    • 2013-04-05
    相关资源
    最近更新 更多