Search code examples
androidimagecrop

Why is my cropped image little?


I'm trying to do a code that allow the user to select a picture from gallery and crop it with an Intent to after all display it in a ImageView (For a profile picture purpose).

Everything is working well, the image cans be selected, it cans be cropped and display in an ImageView which is in a custom dialog. The thing is that the ImageView in my custom dialog is very little, even if I set wrap_content for width and height, so the user can hardly see the cropped image.

Here is a screenshot: Little image

Code:

my custom dialog (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>

In my 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: I want to keep the scropping option


Solution

  • Change your onActivityResult() like this.

    What you are getting back is thumbnail (128x128). You have to use that Uri to get the full image.

    @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?!");
                }
            }
        }
    }