Search code examples
androidonactivityresult

OnActivityResult doesn't update ImageView in Android


In my app I have set image in activity called Day. Now, when the user wants to change the image he choses the button and selects the image from Gallery. Here is the code :

private void choosePhoto() {
    Intent pickPhoto = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(pickPhoto, REQUEST_IMAGE_PICK);
}

When user selects image, I catch in onActivityResult, what he chose, and want to set it as new ImageView, but it doesn't update. I mean, the old image is still visible, but not the new one. I have to quit and enter to activity again to see the change. Here is onActivityResult :

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_PICK && resultCode == RESULT_OK) {
        Uri selectedImage = data.getData();
        String realImagePath = PhotoUtils.getRealPathFromURI(this, selectedImage);
        Bitmap imageBitmap = PhotoUtils.decodeSampledBitmapFromFile(dayPhoto, realImagePath);
        dbAdapter.setDayImage(realImagePath, dayOfMonth, month, year);
        dayPhoto.setImageBitmap(imageBitmap);  // this doesn't work, the image is not updated
    }
}

Solution

  • add below code after dayPhoto.setImageBitmap(imageBitmap);

     dayPhoto.invalidate();