简体   繁体   English

使用“com.android.camera.action.CROP”意图裁剪图像,在某些设备上返回小尺寸位图

[英]Using “com.android.camera.action.CROP” Intent to crop images, returns small size bitmaps on some devices

I am using "com.android.camera.action.CROP" to crop images from user`s image gallery.. 我正在使用“com.android.camera.action.CROP”从用户的图库中裁剪图像。

Crop function: 裁剪功能:

private void performCrop(String picUri) {
        try {
            Intent cropIntent = new Intent("com.android.camera.action.CROP");

            File f = new File(picUri);
            Uri contentUri = Uri.fromFile(f);

            cropIntent.setDataAndType(contentUri, "image/*");
            cropIntent.putExtra("crop", "false");
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            cropIntent.putExtra("outputX", 1024); //512
            cropIntent.putExtra("outputY", 1024); //512

            cropIntent.putExtra("return-data", true);
            startActivityForResult(cropIntent, RESULT_CROP);
        }
        catch (ActivityNotFoundException e) {
            String errorMessage = "your device doesn't support the crop action!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }

and set the result to image view: 并将结果设置为图像视图:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == GALLERY_ACTIVITY_CODE) {
            if(resultCode == Activity.RESULT_OK){
                picturePath = data.getStringExtra("picturePath");

                //perform Crop on the Image Selected from Gallery
                performCrop(picturePath);
            }
        }

        if (requestCode == RESULT_CROP ) {
            if(resultCode == Activity.RESULT_OK){
                Bundle extras = data.getExtras();
                Bitmap selectedBitmap = extras.getParcelable("data");
                // Set The Bitmap Data To ImageView
                addImageImageView.setImageBitmap(selectedBitmap);
                addImageImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            }
        }
    }

On some devices, crop action returns a blurry and small size image (eg. a 4128*2322 pixel image turns to a 160*160 pixel cropped image). 在某些设备上,裁剪操作会返回模糊且小尺寸的图像(例如,4128 * 2322像素图像变为160 * 160像素裁剪图像)。 While on most devices, it works great. 在大多数设备上,它运行良好。 I am not sure what outputX and Y are really do, but changing them doesn`t solve my problem. 我不确定outputX和Y是什么做的,但更改它们并不能解决我的问题。

No, Android Does Not Have a Crop Intent 不,Android的没有一个作物意向

Many developers are calling startActivity() on an Intent with an action of com.android.camera.action.CROP . 许多开发人员使用com.android.camera.action.CROP的操作在Intent上调用startActivity() They are doing this to crop an image. 他们这样做是为了裁剪图像。

This is a really bad idea. 这是一个非常糟糕的主意。

In this specific case, this Intent action is supported by the AOSP Camera app. 在这种特定情况下,AOSP Camera应用程序支持此Intent操作。 That app does not exist on all devices. 该应用程序并非在所有设备上都存在。 Devices lacking this app will not respond to this undocumented Intent action, and your app will crash. 缺少此应用程序的设备将不会响应此未记录的Intent操作,您的应用程序将崩溃。


Solution

There are several open source libraries for cropping images in Android. 有几个用于在Android中裁剪图像的开源库。 I have not tried any of them, and therefore cannot vouch for how well any work. 我没有尝试过任何一种,因此无法保证任何工作的顺利性。 However, they are safer bets than relying upon an undocumented Intent action from an app that may not exist on any given user's device. 但是,它们比依赖任何给定用户设备上可能不存在的应用程序中的未记录的Intent操作更安全。

Here are some libraries to consider: 以下是一些需要考虑的库:

Hope this solved your problem. 希望这能解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用com.android.camera.action.CROP裁剪位图 - how to crop bitmap using com.android.camera.action.CROP 使用com.android.camera.action.CROP的Android圆形裁剪图像在HTC设备中返回黑色图像 - Android circular crop image using com.android.camera.action.CROP returning black image in HTC devices 未找到处理 Intent com.android.camera.action.CROP 的活动 - No activity found to handle Intent com.android.camera.action.CROP com.android.camera.action.CROP卡住 - com.android.camera.action.CROP stucks com.android.camera.action.CROP替代? - com.android.camera.action.CROP alternative? 在android上使用com.android.camera.action.CROP裁剪保存的图像 - Crop saved Image using com.android.camera.action.CROP on android 新的Intent(“ com.android.camera.action.CROP”),我想更改颜色背景,按钮等 - new Intent(“com.android.camera.action.CROP”), I would like change color background, button and other 找不到活动来处理Intent act = com.android.camera.action.CROP - No Activity found to handle Intent act=com.android.camera.action.CROP com.android.camera.action.CROP活动未更新其图像 - com.android.camera.action.CROP activity not updating its image com.android.camera.action.CROP在Motorola Defy上设置壁纸 - com.android.camera.action.CROP sets wallpaper on Motorola Defy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM