android - 裁剪不适用于图库图像

标签 android image crop

我正在使用以下代码从相机和图库中裁剪图像:

private void doCrop() {
    final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();

    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");

    List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 0);

    int size = list.size();

    if (size == 0) {
        Toast.makeText(this, getApplicationContext().getString(R.string.crop_unavailable), Toast.LENGTH_SHORT).show();
        // return
    } else {
        intent.setData(mImageCaptureUri);
        int y = 200;
        if(lastSelect == 1 || lastSelect == 2 || lastSelect == 3 || lastSelect == 4){
            y = (int) (200 * 0.5697329376854599);
        }
        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", y);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);

        if (size == 1) {
            Intent i = new Intent(intent);
            ResolveInfo res = list.get(0);

            i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

            startActivityForResult(i, CROP_FROM_CAMERA);
        } else {
            for (ResolveInfo res: list) {
                final CropOption co = new CropOption();

                co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
                co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
                co.appIntent = new Intent(intent);

                co.appIntent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

                cropOptions.add(co);
            }

            CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(getApplicationContext().getString(R.string.select_crop_app));
            builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int item) {
                    startActivityForResult(cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
                }
            });

            builder.setOnCancelListener(new DialogInterface.OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {

                    if (mImageCaptureUri != null) {
                        getContentResolver().delete(mImageCaptureUri, null, null);
                        mImageCaptureUri = null;
                    }
                }
            });

            AlertDialog alert = builder.create();

            alert.show();
        }
    }
}

虽然它可以很好地裁剪来自相机的图片,但不适用于图库图像。它总是返回从图库中选择的基本图片。

onActivityResult :

if (resultCode != RESULT_OK)
            return;
        switch (requestCode) {
            case PICK_FROM_CAMERA:
                doCrop();
                break;
            case PICK_FROM_FILE:
                mImageCaptureUri = data.getData();
                doCrop();
                break;
            case CROP_FROM_CAMERA:
                Bundle extras = data.getExtras();
                if (extras != null) {
                    photo = extras.getParcelable("data");
//                    imgImage4.setImageBitmap(photo);
                    int w = (int) (UIHelpers.width * 0.15);
                    File f = new File(mImageCaptureUri.getPath());
                    if (f.exists()) {
                    // f.delete();
                    }
                    String imageDir = getRealPathFromURI(mImageCaptureUri);
                            txtSelectLogoNj.setVisibility(View.GONE);
                            rlLogoNj.setVisibility(View.VISIBLE);
                            image5 = imageDir;
                            App.sharedpreferences = getSharedPreferences(App.MyPREFERENCES, Context.MODE_PRIVATE);
                            App.editor = App.sharedpreferences.edit();
                            App.editor.putString("IMAGE5", imageDir);
                            App.editor.commit();
                    }
                }
                break;
        }

我该如何解决?

最佳答案

请使用库项目从相机或画廊裁剪图像。 完美,它会起作用。

https://github.com/biokys/cropimage

关于android - 裁剪不适用于图库图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30976948/

相关文章:

android - 使用 Firebase BaaS 通过 Android 登录 Facebook

android - 从服务器 [DF-AA-20] 检索信息时出错

Java Swing - 如何在Jpanel的北部添加图像

php - 显示没有图像的视频中的 Jwplayer 预览图像

javascript - jcrop 全宽/高,带比例裁剪

android - Android 5.0 (API 21) 上的 Drawable setHotspot 的用途是什么?

android - 如何解决任务 ':app:kaptDebugKotlin'的配置错误?

java - 尝试用 Java 制作骰子滚动动画

image - 从 Matlab 中的单个图像裁剪多个部分

android - 将方形图像裁剪为圆形 - 以编程方式