在Android 4.4中从库中选择图像时剪裁图像

时间:2014-03-25 09:07:02

标签: android android-intent android-4.4-kitkat

我正在尝试为从图库中挑选的图像提供裁剪设施,除了Android 4.4之外它工作正常。如何解决这个问题?我正在使用以下代码

这就是我基于Android版本调用图库意图的方式

ImageView ivGallery = (ImageView) pop.findViewById(R.id.ivGallery);
                ivGallery.setOnClickListener(new OnClickListener()
                {
                    @SuppressLint("InlinedApi")
                    @Override
                    public void onClick(View v)
                    {
                        if (Build.VERSION.SDK_INT < 19)
                        {
                            Intent intent = new Intent();
                            pop.dismiss();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            intent.putExtra("crop", "true");
                            intent.putExtra("return-data", true);
                            intent.putExtra("aspectX", 300);
                            intent.putExtra("aspectY", 300);
                            intent.putExtra("outputX", 300);
                            intent.putExtra("outputY", 300);

                            startActivityForResult(intent, StaticMembers.galleryRequestCode); //1=gallery
                        }
                        else
                        {
                            picUri = ImageUtils.getTempUri();
                            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                            intent.addCategory(Intent.CATEGORY_OPENABLE);
                            intent.setType("image/*");
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
                            pop.dismiss();
                            startActivityForResult(intent, StaticMembers.GALLERY_KITKAT_INTENT_CALLED);
                        }

                    }
                });

这是我的onActivityResult

@SuppressLint("NewApi")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == StaticMembers.galleryRequestCode && resultCode == Activity.RESULT_OK)
        {
            Utils.deleteTempFolder();
            Bundle extras = data.getExtras();
            try
            {
                Thread.sleep(2000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            bmp = extras.getParcelable("data");
            thumbBitmap = Bitmap.createScaledBitmap(bmp, 100, 100, true);
            imgdp.setImageBitmap(bmp);
            setConfirmPicDialog();
        }
        else if (requestCode == StaticMembers.GALLERY_KITKAT_INTENT_CALLED && resultCode == Activity.RESULT_OK)
        {
            Log.d("kitkat", "Inside onActivity result for kitkat");
            picUri = data.getData();
            performCrop(); //what is to be done here?



        }



    }

    private void performCrop()
    {
        try
        {
            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            cropIntent.setDataAndType(picUri, "image/*");
            cropIntent.putExtra("crop", "true");
            cropIntent.putExtra("return-data", true);
            cropIntent.putExtra("aspectX", 300);
            cropIntent.putExtra("aspectY", 300);
            cropIntent.putExtra("outputX", 300);
            cropIntent.putExtra("outputY", 300);
            startActivityForResult(cropIntent, StaticMembers.galleryRequestCode);
        }
        catch (ActivityNotFoundException anfe)
        {
            String errorMessage = "Whoops - your device doesn't support the crop action!";
            Toast toast = Toast.makeText(UserInfoActivity.this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }

为了在Android 4.4

中拥有裁剪意图,应在上面的代码中进行哪些更改

1 个答案:

答案 0 :(得分:1)

使用Gallery的裁剪功能不是一个好习惯。在某些固件中缺少它,因此在某些情况下app甚至可能会崩溃。

相反,您可以使用此库android-cropimage