裁剪后的Android图片视图仅显示小图片

时间:2015-08-08 09:36:19

标签: android image imageview crop

我正在尝试创建一个Android应用程序,允许用户捕获图像,然后将其发送到服务器进行进一步处理,但在此之前,用户需要裁剪图像。我使用下面的代码来调用裁剪意图并在图像视图上显示图像,但遗憾的是裁剪后显示的图像非常小(几乎像缩略图),这里的情况1是选择相机时的情况,情况2是选择来自画廊。

case 1:
            if (resultCode == Activity.RESULT_OK) {
                // get the image uri from earlier
                Uri selectedImage = imageUri;
                // notify any apps of any changes we make
                getContentResolver().notifyChange(selectedImage, null);

                // create a content resolver object which will allow us to access the image file at the uri above
                ContentResolver cr = getContentResolver();
                // create an empty bitmap object
                Bitmap bitmap;

                try {
                    // get the bitmap from the image uri using the content resolver api to get the image
                    bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
                    // set the bitmap to the image view
                    imageView.setImageBitmap(bitmap);
                    // notify the user
                    Toast.makeText(Uploader.this, selectedImage.toString(), Toast.LENGTH_LONG).show();
                   cropimage(selectedImage);


                } catch (Exception e) {
                    // notify the user
                    Toast.makeText(Uploader.this, "failed to load", Toast.LENGTH_LONG).show();
                    Log.e(logtag, e.toString());
                }
            }
            break;

cropimage方法:

public void cropimage(Uri selectedImage)
{
    //indicate image type and Uri     
    cropIntent.setDataAndType(selectedImage, "image/*");
    //set crop properties
    cropIntent.putExtra("crop", "true");
    cropIntent.putExtra("scale", true);
    cropIntent.putExtra("scaleUpIfNeeded",true);
    cropIntent.putExtra("return-data", true);

    startActivityForResult(cropIntent, PIC_CROP);

    return;
}

PIC_CROP保存值3.在onActivityResult中:

 case 3:
                if(resultCode==Activity.RESULT_OK) {
                    //get the returned data
                    Bundle extras = data.getExtras();
                   //get the cropped bitmap
                    Bitmap bitmap = null;
                    if(extras!=null)
                    {
                         bitmap = extras.getParcelable("data");
                    }
                   imageView.setImageBitmap(bitmap);
                  //  imageView.setScaleType(ImageView.ScaleType.FIT_XY);


                }

图像视图上显示的图像非常小。我需要图像视图来显示实际尺寸中的裁剪图像。请帮我解决这个问题。 下面是布局的xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="com.example.kart.counter.Upload_photo">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imgdsp"
    android:layout_gravity="center_horizontal"
    android:adjustViewBounds="true"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Upload and check"
        android:layout_gravity="bottom"
        android:onClick="imageupload"
        />
</LinearLayout>

Cropping image

ImageView displaying small image

1 个答案:

答案 0 :(得分:0)

When you use to crop, the image lost quality and size, for that, to work with ImageView with layout_width="wrap_content" and layout_height="wrap_content" doesn't show good, you need set a size and play with scaleType attribute.

Check http://etcodehome.blogspot.com/2011/05/android-imageview-scaletype-samples.html