Android:裁剪图像并在 ImageView 上显示

Posted

技术标签:

【中文标题】Android:裁剪图像并在 ImageView 上显示【英文标题】:Android: Crop image and Displaying it on ImageView 【发布时间】:2016-11-01 20:47:32 【问题描述】:

我想从图库中选择一个文件,然后尝试裁剪它我面临的问题是:

在裁剪图像和活动结果后,我收到错误,因为我收到 null

结果活动片段

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        if (resultCode != RESULT_OK)
            return;

        switch (requestCode) 
            case PICK_FROM_CAMERA:
                /**
                 * After taking a picture, do the crop
                 */
                doCrop();

                break;

            case PICK_FROM_FILE:
                /**
                 * After selecting image from files, save the selected path
                 */
                Toast.makeText(this,"in gall",Toast.LENGTH_LONG).show();
                mImageCaptureUri = data.getData();
               Log.e("URI", mImageCaptureUri.toString());
                doCrop();

                break;

            case crop:
                Log.e("URI2", mImageCaptureUri.toString());

                Bundle extras = data.getExtras();
                /**
                 * After cropping the image, get the bitmap of the cropped image and
                 * display it on imageview.
                 */

                    Bitmap photo = extras.getParcelable("data");
                    Log.e("URI9", mImageCaptureUri.toString());
                      //here i am receiving null idk why.....
                    mImageView.setImageBitmap(photo);


                //    File f = new File(mImageCaptureUri.getPath());
                /**
                 * Delete the temporary image
                 */
                //  if (f.exists())
                //    f.delete();

                break;

        
    

这是我的 Do Crop 功能

    private void doCrop() 
        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"), crop);

         catch (ActivityNotFoundException e) 
// Do nothing for now
        

        


我已将作物声明为: private static final int crop = 5;

【问题讨论】:

Android does not have a CROP Intent。有很多image cropping libraries available for Android。请使用一个。 我认为这个问题与***.com/questions/27294252/…重复 【参考方案1】:

Ops 我只是忽略了 null 检查一切都很好,新更新的代码,无错误的代码。

     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        if (resultCode != RESULT_OK)
            return;

        switch (requestCode) 
            case PICK_FROM_CAMERA:
                /**
                 * After taking a picture, do the crop
                 */
                doCrop();

                break;

            case PICK_FROM_FILE:
                /**
                 * After selecting image from files, save the selected path
                 */
                Toast.makeText(this,"in gall",Toast.LENGTH_LONG).show();
                mImageCaptureUri = data.getData();
               Log.e("URI", mImageCaptureUri.toString());
                doCrop();

                break;

            case crop:
                Log.e("URI2", mImageCaptureUri.toString());

                Bundle extras = data.getExtras();
                /**
                 * After cropping the image, get the bitmap of the cropped image and
                 * display it on imageview.
                 */
                    if(extras!=null)

                    Bitmap photo = extras.getParcelable("data");
                    Log.e("URI9", mImageCaptureUri.toString());
                    mImageView.setImageBitmap(photo);


                //    File f = new File(mImageCaptureUri.getPath());
                /**
                 * Delete the temporary image
                 */
                //  if (f.exists())
                //    f.delete();

                break;

        
    

【讨论】:

以上是关于Android:裁剪图像并在 ImageView 上显示的主要内容,如果未能解决你的问题,请参考以下文章

Android在父RelativeLayout内裁剪ImageView

在 Android 中,如何移动裁剪的图像? (高级ImageView定位)

图像视图裁剪为特定形状

Android ImageView 将较小的图像缩放到具有灵活高度的宽度,而不会裁剪或扭曲

Android:从位图裁剪位图并设置为 ImageView src

如何在android中以圆形裁剪图像?