[英]Android image view after cropping only displays small image
我正在嘗試制作一個允許用戶捕獲圖像的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>
當您進行裁切時,圖像會失去質量和尺寸,為此,要使用具有layout_width =“ wrap_content”和layout_height =“ wrap_content”的ImageView效果不佳,您需要設置尺寸並使用scaleType屬性。
檢查http://etcodehome.blogspot.com/2011/05/android-imageview-scaletype-samples.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.