Android listview imageview frustration
<p>I've been messing about with this for a long while with still no solution. I'm populating listview images via an asynctask and it works fine in terms of populating but the problem is that when I scroll the list the images disappear for a second or two then reappear. Please note, ALL images are on my SD card, nothing is downloaded. I just adjusted the code from Android dev blog and never renamed it. The relevant portion of my getView looks like this:</p>

<p>image = PATH to image file
image_main = imageView
image_table = Table that holds imageView</p>

<pre><code>                list_image.setVisibility(View.VISIBLE);                 
                ImageDownloader imgDwn = new ImageDownloader();
                imgDwn.download(image, image_main, image_table);
            } else {
                list_image.setVisibility(View.GONE);
                image_table.setVisibility(View.GONE);
                image_main.setImageBitmap(null);
            }
</code></pre>

<p>The class that handles it looks like this:</p>

<p>public class ImageDownloader {</p>

<pre><code>public void download(String url, ImageView imageView, TableLayout imageTable) {
    if (cancelPotentialDownload(url, imageView)) {
    BitmapDownloaderTask task = new BitmapDownloaderTask(imageView, imageTable);
    DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
    imageView.setImageDrawable(downloadedDrawable);
    task.execute(url);
    }
}

class BitmapDownloaderTask extends AsyncTask&lt;String, Void, Bitmap&gt; {
    String url;
    private final WeakReference&lt;ImageView&gt; imageViewReference;
    private final WeakReference&lt;TableLayout&gt; imageTableReference;

    public BitmapDownloaderTask(ImageView imageView, TableLayout imageTable) {
        imageViewReference = new WeakReference&lt;ImageView&gt;(imageView);
        imageTableReference = new WeakReference&lt;TableLayout&gt;(imageTable);
    }

      @Override
      protected Bitmap doInBackground(String... params) { 
             BitmapFactory.Options o = new BitmapFactory.Options();
              o.inJustDecodeBounds = true;
              BitmapFactory.decodeFile(params[0], o);
              final int REQUIRED_SIZE=70;

              //Find the correct scale value. It should be the power of 2.
              int width_tmp=o.outWidth, height_tmp=o.outHeight;
              int scale=4;
              while(true){
                  if(width_tmp/2&lt;REQUIRED_SIZE || height_tmp/2&lt;REQUIRED_SIZE)
                      break;
                  width_tmp/=2;
                  height_tmp/=2;
                  scale++;
              }
              //Decode with inSampleSize
              BitmapFactory.Options o2 = new BitmapFactory.Options();
              o2.inSampleSize=scale;       
              return BitmapFactory.decodeFile(params[0], o2);
      }

      @Override
      protected void onPostExecute(Bitmap result) {
            if (isCancelled()) {
                result = null;
            }

            if (imageViewReference != null) {
                ImageView imageView = imageViewReference.get();
                TableLayout imageTable = imageTableReference.get();
                BitmapDownloaderTask bitmapDownloaderTask = ImageDownloader.getBitmapDownloaderTask(imageView);
                // Change bitmap only if this process is still associated with it
                if (this == bitmapDownloaderTask) {
                      imageView.setImageBitmap(result);
                      imageView.setVisibility(View.VISIBLE);
                      imageTable.setVisibility(View.VISIBLE);
                }              
            }
        }
}

static class DownloadedDrawable extends ColorDrawable {
    private final WeakReference&lt;BitmapDownloaderTask&gt; bitmapDownloaderTaskReference;

    public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
        super(Color.BLACK);
        bitmapDownloaderTaskReference =
            new WeakReference&lt;BitmapDownloaderTask&gt;(bitmapDownloaderTask);
    }

    public BitmapDownloaderTask getBitmapDownloaderTask() {
        return bitmapDownloaderTaskReference.get();
    }
}

private static boolean cancelPotentialDownload(String url, ImageView imageView) {
    BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);

    if (bitmapDownloaderTask != null) {
        String bitmapUrl = bitmapDownloaderTask.url;
        if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) {
            bitmapDownloaderTask.cancel(true);
        } else {
            // The same URL is already being downloaded.
            return false;
        }
    }
    return true;
}

private static BitmapDownloaderTask getBitmapDownloaderTask(ImageView imageView) {
    if (imageView != null) {
        Drawable drawable = imageView.getDrawable();
        if (drawable instanceof DownloadedDrawable) {
            DownloadedDrawable downloadedDrawable = (DownloadedDrawable)drawable;
            return downloadedDrawable.getBitmapDownloaderTask();
        }
    }
    return null;
}
</code></pre>

<p>}</p>
