title: Using AsyncTask to load images into a custom adapter
body: <p>Although there are many tutorials out there, I've found it really difficult to implement an AsyncTask to load images from URI's (obtained from a content provider) into a custom adapter.</p>
  
  <p>I get the basic gist, which is to have a class containing an AsyncTask, do the bitmap creation in the 'doInBackground', and then set the ImageView in the onPostExecute.</p>
  
  <p>The problem for me, being new to android &amp; programming, is that I don't know how to pass in my Uri's to the AsyncTask for each item, how to create the bitmap, and how to return it to the adapter.</p>
  
  <p>I've only gotten this far with the actual AsyncTask class (ImageLoader):</p>
  
  <pre><code>package another.music.player;
  
  import android.graphics.Bitmap;
  import android.os.AsyncTask;
  import android.widget.ImageView;
  
  public class ImageLoader extends AsyncTask&lt;String, String, Bitmap&gt; {
  
      private ImageView imageView;
      private Bitmap bitmap = null;
  
      @Override
      protected Bitmap doInBackground(String... uri) {
  
          // Create bitmap from passed in Uri here
  
          return bitmap;
  
      }
  
      @Override
      protected void onPostExecute(Bitmap bitmap) {
          if (bitmap != null &amp;&amp; imageView != null) {
              imageView.setImageBitmap(bitmap);
  
          }
      }
  }
  </code></pre>
  
  <p>And my custom adapter looks like this:</p>
  
  <pre><code>package another.music.player;
  
  import android.content.ContentUris;
  import android.content.Context;
  import android.database.Cursor;
  import android.net.Uri;
  import android.provider.BaseColumns;
  import android.provider.MediaStore.Audio.AlbumColumns;
  import android.provider.MediaStore.Audio.AudioColumns;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.CursorAdapter;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  class AlbumAdapter extends CursorAdapter {
  
      public AlbumAdapter(Context context, Cursor c, int flags) {
          super(context, c, flags);
      }
  
      private static Uri currentSongUri;
  
      @Override
      public void bindView(View view, Context context, Cursor cursor) {
          ImageView albumArt = (ImageView) view.getTag(R.id.albumArt);
          TextView text1 = (TextView) view.getTag(R.id.artistTitle);
          TextView text2 = (TextView) view.getTag(R.id.albumTitle);
          TextView text3 = (TextView) view.getTag(R.id.totalSongs);
  
          albumArt.setImageBitmap(null);
  
          text1.setText(cursor.getString(cursor
                  .getColumnIndex(AudioColumns.ARTIST)));
          text2.setText(cursor.getString(cursor
                  .getColumnIndex(AudioColumns.ALBUM)));
          text3.setText(cursor.getString(cursor
                  .getColumnIndex(AlbumColumns.NUMBER_OF_SONGS)));
  
          String currentAlbumId = cursor.getString(cursor
                  .getColumnIndex(BaseColumns._ID));
  
          Integer currentAlbumIdLong = Integer.parseInt(currentAlbumId);
          Uri artworkUri = Uri.parse("content://media/external/audio/albumart");
          currentSongUri = ContentUris.withAppendedId(artworkUri,
                  currentAlbumIdLong);
  
          //Run ImageLoader AsyncTask here, and somehow retrieve the ImageView &amp; set it.
  
      }
  
      @Override
      public View newView(Context context, Cursor cursor, ViewGroup parent) {
          LayoutInflater inflater = LayoutInflater.from(context);
          View view = inflater.inflate(R.layout.albumitem, null);
          view.setTag(R.id.albumArt, view.findViewById(R.id.albumArt));
          view.setTag(R.id.artistTitle, view.findViewById(R.id.artistTitle));
          view.setTag(R.id.albumTitle, view.findViewById(R.id.albumTitle));
          view.setTag(R.id.totalSongs, view.findViewById(R.id.totalSongs));
  
          return view;
      }
  
  }
  </code></pre>
  
  <p>I would be very grateful if somebody could show me how to proceed with this.</p>
  
  <p>Thanks.</p>