Harsh Raval Harsh Raval - 4 years ago 84
Android Question

how to get pic for each song? i come to know that its known as album art . i get song name , artist, album from my code but not album art

String[] STAR = {"*"};

Cursor cursor;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";


cursor = getContentResolver().query(uri, STAR, selection, null, null);

if (cursor != null) {

if (cursor.moveToFirst()) {
int i = 0;
Drawable img;
do {
String songName = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));

path[i] = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));

String albumName = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
albumId = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
song.add(songName);


//above code works perfectly but this next line creates error

String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
System.out.println(path);
i++;
}
while (cursor.moveToNext());

Answer Source

You can use the following method to get album art of songs :

public static Bitmap getAlbumart(Context context, Long album_id){
    Bitmap bm = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    try{
          final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
          Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
          ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
          if (pfd != null){
              FileDescriptor fd = pfd.getFileDescriptor();
              bm = BitmapFactory.decodeFileDescriptor(fd, null, options);
              pfd = null;
              fd = null;
          }
      } catch(Error ee){}
      catch (Exception e) {}
      return bm;
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download