title: How to force a cache clearing using Universal Image Loader Android?
body: <p>I am using UIL to load images in a listview. </p>
  
  <p>When I long press an image in the listview, I show a dialog to modify that picture, replacing it with a new one using the camera.</p>
  
  <p>If I take a new picture, when the dialog is dismissed my listview still shows the old image (since it is cached). If I close and restart my application when I go to my listview the new image is correctly there.</p>
  
  <p>This is how I set up UIL:</p>
  
  <pre><code>// Get singletone instance of ImageLoader
      imageLoader = ImageLoader.getInstance();
  
      //set display options for image loader
      DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
      .cacheInMemory()
      .displayer(new FadeInBitmapDisplayer(500)) //fade in images
      .resetViewBeforeLoading()
      .build();
  
      //set image loader options
      ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).defaultDisplayImageOptions(displayOptions).build();
  
      // Initialize ImageLoader with configuration.
      imageLoader.init(config);
  </code></pre>
  
  <p>If I remove <code>.cacheInMemory()</code> everything works though. I am just wondering if I can clear the cache when opening my dialog only. I tried to get hold of the ImageView selected and call <code>myImageView.invalidate()</code> when opening the dialog without success. </p>
  
  <p>The images are loaded from file:</p>
  
  <pre><code>// Load and display image asynchronously
  imageLoader.displayImage(file_prefix + image_path, image);
  </code></pre>
  
  <p>Any suggestions?</p>
  
  <p>Edit: code to create  a context menu when long pressing an image, I tried to clear the cache there:</p>
  
  <pre><code>@Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
      super.onCreateContextMenu(menu, v, menuInfo);
  
      //get info about item selected
      AdapterView.AdapterContextMenuInfo info;
      try {
          // Casts the incoming data object into the type for AdapterView objects.
          info = (AdapterView.AdapterContextMenuInfo) menuInfo;
      } catch (ClassCastException e) {
          // If the menu object can't be cast, logs an error.
          Log.e("no info", "bad menuInfo", e);
          return;
      }
      Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
      if (cursor == null) {
          // For some reason the requested item isn't available, do nothing
          return;
      }
  
      //remove selected image from cache (if it is an image)
      imageUrl = cursor.getString(cursor.getColumnIndex("image_path"));
      if (!imageUrl.equalsIgnoreCase("")) {
          MemoryCacheUtil.removeFromCache(imageUrl, imageLoader.getMemoryCache());
      }
  
      Log.i("imageUrl", imageUrl);
  
      //get defect row ID and text content to pass it to defect activity
      defect_row_id = cursor.getLong(cursor.getColumnIndex("_id"));
      defect_txt = cursor.getString(cursor.getColumnIndex("defect"));
  
      MenuInflater inflater = getMenuInflater();
  
      Log.i("cursor", DatabaseUtils.dumpCursorToString(cursor));
  
      //set project identifier in context menu header, mapping cursor sequence of values
      menu.setHeaderTitle(getString(R.string.select_an_option));
      inflater.inflate(R.menu.menu_defect_row, menu);
  
  }
  </code></pre>
  
  <p>When a menu item (edit or delete) is selected</p>
  
  <pre><code>@Override
  public boolean onContextItemSelected(MenuItem item) {
      //AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
      switch (item.getItemId()) {
  
      case R.id.edit:
  
          //open defect activity with the specified image and defect pre-loaded
          Intent editDefectIntent = new Intent(this, DefectActivity.class);
  
          editDefectIntent.putExtra("defect_row_id", defect_row_id);
          editDefectIntent.putExtra("imageUrl", imageUrl);
  
          startActivity(editDefectIntent);
  
          return true;
      case R.id.delete:
  
          askDeleteConfirm();
  
          return true;
  
      default:
          return false;
      }
  
  }//onContextItemSelected
  </code></pre>
  
  <p>Edit: code to display list of images</p>
  
  <pre><code>@Override
  public void onResume() {
      super.onResume();
  
      //open connection to db
      db = new DBAdapter(this);
      db.open();
  
      Log.i("DefectListActivity -&gt; onResume", "called");
  
      // get all defects for this unit
      defectList = db.getAllDefectsByUnit(unit_id);
      // create an array adapter and let it to display our row
      defects = new SimpleCursorAdapter(this, R.layout.defect_row, defectList, new String[] { "defect", "image_path" }, new int[] { R.id.defect, R.id.image }, 0);
  
      //set custom view using ViewBinder
      SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
          @Override
          public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
  
              int placeholder_id = getResources().getIdentifier("placeholder", "drawable", getPackageName());
  
              //get column name
              String name = cursor.getColumnName(columnIndex);
  
              //for the thumbnail column,if we have an image replace the placeholder
              if ("image_path".equals(name)) {
  
                  ImageView image = (ImageView) view.findViewById(R.id.image);
                  //Bitmap thumbnail;
                  String image_path = cursor.getString(columnIndex);
  
                  Log.i("image_path -&gt;", image_path);
  
                  if (!image_path.equalsIgnoreCase("")) {
  
                  // Load and display image asynchronously
                  imageLoader.displayImage(file_prefix + image_path, image);
  
                  } else {
  
                      image.setImageResource(placeholder_id);
  
  
                      }
  
                  return true;
  
              }
  
              //for the defect column, just add the text to the view
              if ("defect".equals(name)) {
  
                  String defect_text = cursor.getString(columnIndex);
  
                  TextView defect_holder = (TextView) view.findViewById(R.id.defect);
                  defect_holder.setText(defect_text);
  
                  return true;
              }
  
              return false;
          }
      };
  
      defects.setViewBinder(binder);
  
      setListAdapter(defects);
  
  }//onResume
  </code></pre>