在不使用意图的情况下在android中裁剪图像

时间:2016-02-25 09:48:01

标签: android

目前我正在使用默认的Android相机意图来裁剪图像。但是这在某些设备中无法正常工作。在纯Android设备中,不会调用裁剪或返回数据为空。有没有其他方法可以达到同样的目的。

目前我使用以下代码开始裁剪,

Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setType("image/*");
if(picUri != null) {
    cropIntent.setDataAndType(picUri, "image/*");
}
String tmpName = imageDir.getAbsolutePath() + File.separator + "img.jpg";
File tmpFile = new File(tmpName);
if (tmpFile.exists()) {
    tmpFile.delete();
}
Uri tmpUri = Uri.fromFile(tmpFile);
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 16);
cropIntent.putExtra("aspectY", 9);
cropIntent.putExtra("outputX", 320);
cropIntent.putExtra("outputY", 180);
cropIntent.putExtra("return-data", true);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, tmpUri);
startActivityForResult(cropIntent, PIC_CROP);

2 个答案:

答案 0 :(得分:3)

查看this库。这支持在不使用相机裁剪意图的情况下裁剪图像。它还支持许多其他功能,如旋转等。它易于实现

提供的sample也写得很好。

答案 1 :(得分:-1)

如果图像可以加载到内存中,您可以使用BitmapFactory.decodeFile加载到位图,然后使用createBitmap对位图进行裁剪。小心使用任意图像执行此操作,因为如果内存不足以加载图像,则解码将崩溃。