I am building an android where. Inside of one activity I have an image button. When I click on it the gallery opens up and I can choose an image. Then I set that image as the new image for the image button.
The problem is the image appears way too big inside my activity. How can I make it fit into my image button?
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
mImageButton.setImageBitmap(yourSelectedImage);
}
}
}
You can use this method to get a resized image. This way you can avoid OutOfMemoryError
requiredSize means either one of height or width. Based on this code:
That means, if photo is landscape, 1000×800, and you put required size as 500, then the resulting image will be 500×400. And if the photo is portrait, 800×1000, and requiredSize specified as 500, the resulting image then will be 400×500.