title: Retrieving Bitmap from ListView
body: <p>My application downloads data(images and text) from web, and shows it as ListView. in ListView adapter I have implemented memory cache to store Bitmaps. And the main purpose to retrieve image from ListView is to pass it to new Activity after click on row. I know that I can pass Image URL to new Activity and then download it again, but I don't want to do like that.
  Is it possible to retrieve Bitmap from ListView? If yes, which way will be most efficienteasy to implement?
  Thanks in advance</p>
  
  <p>Edit:</p>
  
  <pre><code>public class CinemasAdapter extends ArrayAdapter&lt;Cinemas&gt; {
  
      private ArrayList&lt;Cinemas&gt; movieDataItems;  
      private Activity context;
  
      private LinkedHashMap&lt;String, Bitmap&gt; bitmapCache = new LinkedHashMap&lt;String, Bitmap&gt;();
  
      public CinemasAdapter(Activity context, int textViewResourceId, ArrayList&lt;Cinemas&gt; movieDataItems) {
          super(context, textViewResourceId, movieDataItems);
          this.context = context;
          this.movieDataItems = movieDataItems;
      }
  
       @Override
      public View getView(int position, View convertView, ViewGroup parent) { 
          ViewHolder holder = new ViewHolder();
          if (convertView == null) {
              LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
              convertView = vi.inflate(R.layout.movie_data_row, null);
              holder.poster = (ImageView) convertView.findViewById(R.id.movie_thumb_icon);
              holder.bitmap = BitmapFactory.decodeResource(holder.poster.getResources(), R.drawable.no_image);
              convertView.setTag(holder);         
          }
          else{
              holder = (ViewHolder) convertView.getTag();
          }
  
          Cinemas movie = movieDataItems.get(position);
  
          if (movie!=null){
                      String url = movie.poster();
  
                          if (url != null) {
                              Bitmap bitmap = fetchBitmapFromCache(url);
                              if (bitmap == null) {
                                  loadBitmap(url,holder.poster,holder.bitmap);
                              }
                              else {
                                  holder.poster.setImageBitmap(bitmap);
                              }
                          }
                          else {
                              holder.poster.setImageResource(R.drawable.no_image);
                          }
              }
          else {
              holder.poster.setImageResource(R.drawable.no_image);
              } 
  
          return convertView;
  
      }           
  
      private void addBitmapToCache(String url, Bitmap bitmap) {
  
          if (bitmap != null) {
              synchronized (bitmapCache) {
                  bitmapCache.put(url, bitmap);
              }
          }
      } 
  
      private Bitmap fetchBitmapFromCache(String url) {
          synchronized (bitmapCache) {
              final Bitmap bitmap = bitmapCache.get(url);
               if (bitmap != null) {
                  bitmapCache.remove(url);
                  bitmapCache.put(url, bitmap);
                  return bitmap;
              } 
          }
          return null;
      } 
  
      private class BitmapDownloaderTask extends AsyncTask&lt;String, Void, Bitmap&gt; {
  
          private String url;
          private final WeakReference&lt;ImageView&gt; imageViewReference;
          private Cinemas movie;
  
          public BitmapDownloaderTask(ImageView imageView, Cinemas mov) {
              imageViewReference = new WeakReference&lt;ImageView&gt;(imageView);   
              movie = mov;
          }
  
          @Override
          protected Bitmap doInBackground (String... source) {
              url = source[0];
              Bitmap image;
              try{
                  image = BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
                  return image;
                  }
              catch(Exception e){Log.e("Error", e.getMessage()); e.printStackTrace();}
              return null;
              } 
  
  
           @Override
          protected void onPostExecute(Bitmap bitmap) { 
              if (isCancelled()) {
                   bitmap = null;
               }
              if (imageViewReference != null &amp;&amp; bitmap != null) {
                   final ImageView imageView = imageViewReference.get();
                   final BitmapDownloaderTask bitmapWorkerTask =
                           getBitmapWorkerTask(imageView);
                   if (this == bitmapWorkerTask &amp;&amp; imageView != null) {
                       imageView.setImageBitmap(bitmap);
                       addBitmapToCache(url);
                       movie.setBitmap(bitmap);
                   }
               }
          } 
      } 
  
      private static class ViewHolder{
      ImageView poster;
      Bitmap bitmap;
  }
  
      static class AsyncDrawable extends BitmapDrawable {
      private final WeakReference&lt;BitmapDownloaderTask&gt; bitmapWorkerTaskReference;
  
      public AsyncDrawable(Resources res, Bitmap bitmap,
              BitmapDownloaderTask bitmapWorkerTask) {
              super(res, bitmap);
              bitmapWorkerTaskReference = new WeakReference&lt;BitmapDownloaderTask&gt;(bitmapWorkerTask);
      }
  
      public BitmapDownloaderTask getBitmapWorkerTask() {
          return bitmapWorkerTaskReference.get();
      }
  }
  
      private static BitmapDownloaderTask getBitmapWorkerTask(ImageView imageView) {
         if (imageView != null) {
             final Drawable drawable = imageView.getDrawable();
             if (drawable instanceof AsyncDrawable) {
                 final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
                 return asyncDrawable.getBitmapWorkerTask();
             }
          }
          return null;
      }
  
      public static boolean cancelPotentialWork(String data, ImageView imageView) {
      final BitmapDownloaderTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
  
      if (bitmapWorkerTask != null) {
          final String bitmapData = bitmapWorkerTask.url;
          if (bitmapData != data) {
              bitmapWorkerTask.cancel(true);
          } else {
              return false;
          }
      }
      return true;
  }
  
      public void loadBitmap(String resId, ImageView imageView, Bitmap bitmap) {
      if (cancelPotentialWork(resId, imageView)) {
          final BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
          final AsyncDrawable asyncDrawable = new AsyncDrawable(imageView.getResources(), bitmap, task);
          imageView.setImageDrawable(asyncDrawable);
          task.execute(resId);
      }
  }
  }
  </code></pre>