Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView not refreshing/reflecting changes

Tags:

I'm using a photo picker intent to choose an image and write it to an application-private file. Most of my important source code is shown below. Once I press a button and perform the first image selection intent, then it successfully updates the image view. However, once I press the image selection button again (in the same activity), then the image view does NOT update, unless I exit and restart the activity. So I know the image is getting successfully saved, but why would the ImageView in the layout not refresh or update?

public void onResume() {     super.onResume();     ImageView myImageView = (ImageView)findViewById(R.id.contact_image);     if (hasImage) {       myImageView.setImageURI(Uri.fromFile(getFileStreamPath(TEMP_PHOTO_FILE)));     }   }    @Override   public void onActivityResult(int requestCode, int resultCode, Intent data) {     switch (requestCode) {     case PHOTO_PICKED:       if (resultCode == RESULT_OK) {         if (data != null) {           Bundle extras = data.getExtras();           if (extras != null) {             hasImage = true;             bmp = (Bitmap) extras.get("data");           }         }       }       break;     }   }    private OnClickListener mChooseImage = new OnClickListener() {     @Override     public void onClick(View v) {       try {         // Launch picker to choose photo for selected contact         Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);         intent.setType("image/*");         intent.putExtra("crop", "true");         intent.putExtra("aspectX", 1);         intent.putExtra("aspectY", 1);         intent.putExtra("outputX", ICON_SIZE);           intent.putExtra("outputY", ICON_SIZE);         intent.putExtra("scale", true);         intent.putExtra("return-data", false);         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile()));         intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());         startActivityForResult(intent, PHOTO_PICKED);       } catch (ActivityNotFoundException e) {         // LOG THIS       }     }   };  private File getTempFile() {     try {       if (!hasImage) {         FileOutputStream fos = openFileOutput(TEMP_PHOTO_FILE, MODE_WORLD_WRITEABLE);         fos.close();       }       return getFileStreamPath(TEMP_PHOTO_FILE);     } catch (FileNotFoundException e) {       // To be logged later       return null;     } catch (IOException e) {       // To be logged later       return null;     }   } 

upon activity result, I set the ImageView's image URI to this file.

When it first completes, the ImageView changes to reflect this. However, if I attempt to choose the image again (same activity), the ImageView will not update until I exit and re-enter the activity. I'm not sure why this happens, is it because I'm trying to write to the temp.jpg everytime? Or do I need to refresh my layout somehow to reflect changes in the ImageView?

like image 355
damonkashu Avatar asked Dec 16 '10 08:12

damonkashu


2 Answers

Judging by the ImageView source code, the ImageView won't reload the image if you call setImageURI with the same URI. You could try changing the URI by writing your image to another file.

like image 101
claesv Avatar answered Sep 28 '22 06:09

claesv


ImageView will not redraw if "new" URI is the same like old one. "View.invalidate()" will not work. To "force" update you can do:

public void onResume() {   super.onResume();   ImageView myImageView = (ImageView)findViewById(R.id.contact_image);   if (hasImage) {     myImageView.setImageDrawable(null); // <--- added to force redraw of ImageView     myImageView.setImageURI(Uri.fromFile(getFileStreamPath(TEMP_PHOTO_FILE)));   } } 
like image 31
mdolanci Avatar answered Sep 28 '22 06:09

mdolanci