0

When I pick an image from the gallery it is too large and I need to resize it. Can anyone give me suggestions on how I might be able to accomplish this?

See code below:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) {
        Uri uri = data.getData();  
        try {
            bitmap = Media.getBitmap(this.getContentResolver(), uri);



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}





        imageView.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {               
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_GALLERY);

                imageView.setImageBitmap(bitmap);

            }
        });
Gathios
  • 39
  • 4

1 Answers1

0

This post has some excellent samples for you: How to Resize a Bitmap in Android?

In your case, this method helps

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    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);
    return resizedBitmap;
}

Edit

Also from the same post, this fits your need in an easier way

Bitmap resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, false);

Update

You can use the code in this way:

// Set the new width and height you want
int newWidth;
int newHeight;
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) {
        Uri uri = data.getData();  
        try {
            bitmap = Media.getBitmap(this.getContentResolver(), uri);
            bitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Community
  • 1
  • 1
chinglun
  • 637
  • 5
  • 18