I have a form where user can click image and set it in imageview.When phone is kept in portrait mode and image is clicked then everything works fine, clicked image is set in imageview. Problem is this that if user click on capture image button in portrait mode and then rotate phone to click image then in onActivityResult I get bitmap null. Is there any solution for this.
Here is my onActivtiyResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 1:
try {
bitmap = AppUtils.decodeFile(file.getPath(), 100, 100);
registrationBean.setImage(file.getPath());
imageList.add(file.getPath());
AppUtils.writeList(this,imageList,"images");
}catch (Exception e) {
e.printStackTrace();
}
if (bitmap != null) {
iv_profile.setImageBitmap(bitmap);
retake_photo.setAlpha(1);
retake_photo.setClickable(true);
take_photo.setClickable(false);
take_photo.setAlpha((float) 0.60);
}
break;
case 2:
try {
String uriImage;
imageUri = imageReturnedIntent.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageStream.close();
String path = getPath(imageUri);
imageList.add(path);
AppUtils.writeList(getBaseContext(), imageList, "images");
registrationBean.setImage(path);
if (selectedImage != null) {
iv_profile.setImageBitmap(selectedImage);
retake_photo.setAlpha(1);
}
uriImage = imageUri.toString();
registrationBean.setImage(uriImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (OutOfMemoryError e)
{
e.printStackTrace();
}
break;
}
}
Method where startActivityForResult is called
private void selectImage() {
final CharSequence[] options = {"Take a Picture", "Choose from Gallery", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(ActivityForm.this);
builder.setCancelable(false);
builder.setTitle("Select Profile Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take a Picture")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i = appSharedPreference.getImageNumber();
file = new File(Environment.getExternalStorageDirectory(), "temp" + i + ".jpg");
i++;
appSharedPreference.setImageNumber(i);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
img_path = Environment.getExternalStorageDirectory() + "/temp.jpg";
startActivityForResult(intent, 1);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
if (options[item].equals("Choose from Gallery")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent photoPickerIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
photoPickerIntent.addCategory(photoPickerIntent.CATEGORY_OPENABLE);
startActivityForResult(photoPickerIntent, 2);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
} else {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
img_path = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString();
startActivityForResult(intent, 2);
}
}
}
});
builder.show();
}