I get an image from the camera or gallery and set it to my imageview. Unfortunately when the screen gets rotated the imageview shows nothing. How can I keep the image visible ?
This is the code where I get the image :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case REQUEST_IMAGE_CAPTURE:
// if taking
if (resultCode == RESULT_OK && null != data && TAKE_OR_PICK == 1) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
int width = imageBitmap.getWidth();
int height = imageBitmap.getHeight();
if(height>width) {
int crop = (height - width) / 2;
Bitmap cropImg = Bitmap.createBitmap(imageBitmap, crop, 0, width, width);
image.setImageBitmap(cropImg);
}
else if (width>height){int crop = (width - height) / 2;
Bitmap cropImg = Bitmap.createBitmap(imageBitmap, crop, 0, height, height);
image.setImageBitmap(cropImg);}
else {image.setImageBitmap(imageBitmap);}
}
break;
case RESULT_LOAD_IMAGE:
// if choosing
if (resultCode == RESULT_OK && null != data && TAKE_OR_PICK == 2) {
Uri selectedImage = data.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 picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap imageBitmap = BitmapFactory.decodeFile(picturePath);
int width = imageBitmap.getWidth();
int height = imageBitmap.getHeight();
if(height>width) {
int crop = (height - width) / 2;
Bitmap cropImg = Bitmap.createBitmap(imageBitmap, crop, 0, width, width);
image.setImageBitmap(cropImg);
}
else if (width>height){int crop = (width - height) / 2;
Bitmap cropImg = Bitmap.createBitmap(imageBitmap, crop, 0, height, height);
image.setImageBitmap(cropImg);
}
else {image.setImageBitmap(imageBitmap);}
}
I've tried to use onSaveInstanceState() but can't get it to work for a bitmap. Is there another way to achieve this ?