【问题标题】:Android image view after cropping only displays small image裁剪后的Android图像视图仅显示小图像
【发布时间】:2015-08-08 09:36:19
【问题描述】:

我正在尝试制作一个 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;

裁剪方法:

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>

【问题讨论】:

    标签: android image imageview crop


    【解决方案1】:

    当您使用裁剪时,图像会丢失质量和大小,为此,要使用具有 layout_width="wrap_content" 和 layout_height="wrap_content" 的 ImageView 效果不佳,您需要设置大小并使用 scaleType属性。

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

    【讨论】:

      猜你喜欢
      • 2016-05-04
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      相关资源
      最近更新 更多