为什么我的裁剪图像很小?

时间:2017-12-15 16:22:26

标签: android image crop

我正在尝试创建一个代码,允许用户从图库中选择一张图片并使用Intent裁剪,然后将其显示在ImageView中(用于个人资料图片)。

一切运行良好,图像可以选择,可以裁剪并显示在自定义对话框中的ImageView中。问题是我的自定义对话框中的ImageView非常小,即使我为宽度和高度设置了wrap_content,因此用户几乎看不到裁剪的图像。

以下是截图:Little image

代码:

我的自定义对话框(custom_dialog_pp.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/style_layout_rounded_white">


<TextView
    android:id="@+id/useit_customdialog_pp_TV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:gravity="center"
    android:text="@string/usethispic"
    android:textSize="25sp"
    android:textColor="@color/white"
    android:background="@drawable/style_tv_toproundcorner"/>

<ImageView
    android:contentDescription="@string/profilpic"
    android:id="@+id/pp_customdialog_pp_IV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

/>

<Space
    android:layout_width="match_parent"
    android:layout_height="10dp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/cancel_customdialog_pp_BTN"
        android:background="@drawable/style_button_primary"
        android:text="@string/cancel"
        />

    <Space
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/done_customdialog_pp_BTN"
        android:background="@drawable/style_button_primary"
        android:text="@string/done"
        />

</LinearLayout>

在我的MainActivity中:

 ////////////////////
ChangeProfilPic.onClickListener {
 Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, 2);
}
 ////////////////////

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 4 && resultCode == RESULT_OK && data != null) {

        Bundle extras = data.getExtras();
        Bitmap selectedImage = extras.getParcelable("data");


        android.support.v7.app.AlertDialog.Builder mBuilderLoading = new android.support.v7.app.AlertDialog.Builder(MainActivity.this);
        View mViewLoading = getLayoutInflater().inflate(R.layout.custom_dialog_pp,null);

        final ImageView pp_customdialog_pp_IV = mViewLoading.findViewById(R.id.pp_customdialog_pp_IV);

        pp_customdialog_pp_IV.setImageBitmap(selectedImage);


        mBuilderLoading.setView(mViewLoading);
        final android.support.v7.app.AlertDialog dialogLoading = mBuilderLoading.create();
        dialogLoading.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialogLoading.show();


    } else if(requestCode == 2 && resultCode == RESULT_OK && data != null) {
        ImageCropFunction(data.getData());
    }
}


public void ImageCropFunction(Uri imguri) {
    try {
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        cropIntent.setDataAndType(imguri, "image/*");
        cropIntent.putExtra("crop", "true");
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        cropIntent.putExtra("outputX", 128);
        cropIntent.putExtra("outputY", 128);
        cropIntent.putExtra("return-data", true);
        startActivityForResult(cropIntent, 4);

    } catch (ActivityNotFoundException ignored) {

    }

Ps:我想保留scropping选项

2 个答案:

答案 0 :(得分:0)

我认为默认行为是在返回结果中返回缩略图。因此,您可能需要使用URI来获取返回的完整大小的图像。

答案 1 :(得分:0)

像这样更改onActivityResult()

你得到的是缩略图(128x128)。您必须使用Uri来获取完整图像。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 2 && resultCode == RESULT_OK && data != null) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        if (selectedImage == null) {
            return;
        }

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);

        if (cursor == null) {
            return;
        }

        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();


        if (picturePath != null) {
            Log.d("TAG", "picturePath " + picturePath);

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options);

            if(bitmap != null) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // you can change the quality here. for dialog, you might only want 50 instead of 100..
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

                AlertDialog.Builder mBuilderLoading = new AlertDialog.Builder(MainActivity.this);
                View mViewLoading = getLayoutInflater().inflate(R.layout.custom_dialog_pp,null);

                final ImageView pp_customdialog_pp_IV = mViewLoading.findViewById(R.id.pp_customdialog_pp_IV);
                pp_customdialog_pp_IV.setImageBitmap(bitmap);
                mBuilderLoading.setView(mViewLoading);

                final AlertDialog dialogLoading = mBuilderLoading.create();
                dialogLoading.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialogLoading.show();
            } else {
                Log.e("TAG", "bitmap is null?!");
            }
        }
    }
}