How to set size of Image when imported in android

I am using the following code to let a user import an image from their gallery into the app.

 public void loadImagefromGallery(View view) {
    // Create intent to Open Image applications like Gallery, Google Photos
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            // Get the cursor
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imgDecodableString = cursor.getString(columnIndex);
            cursor.close();
            ImageView imgView = (ImageView) findViewById(R.id.imgView);
            // Set the Image in ImageView after decoding the String
            imgView.setImageBitmap(BitmapFactory
                    .decodeFile(imgDecodableString));

        } else {
            Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }

But, I want the size of the image to be a certain size no matter what the image size they have imported is. Whenever I import an image it always fill the whole screen. Let's say I want the image size to be 50 width by 50 height.

Answers


hi there are two ways to set the imageview size

  1. from the xml
<ImageView
    android:id="@+id/image_view"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:scaleType="fitCenter"
    android:src="@drawable/iclauncher" 
    android:scaleType="fitXY"/>
  1. from programmatically
image_view.getLayoutParams().height = 20;
image_view.getLayoutParams().width = 20;

this is the way to resize bitmap image

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(
        bm, 0, 0, width, height, matrix, false);
    bm.recycle();
    return resizedBitmap;
}

Happy coding


As suggested by saeed:

// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(decodeUri(this, Uri.parse(imgDecodableString), 50));

Where decodeUri is from here.


Need Your Help

Saving Blob Object As a File On Server

php jquery magento blob

Using jQuery plugin called cropper, I am able to retrieve the cropped image as blob object. Now I need to save this blob object as a file on my server. The code for that is: