androidalbumart

Loading album art efficiently Android


I've been having trouble loading all of the album art located on the phone's SD card efficiently. I can get it all, but it can take upwards of 15-20 seconds I believe because of the Bitmap decoding process. I also don't want to lose the order, if possible. Here's my code below:

    public String[] getAudioList() {
    final Cursor mCursor = getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[]{MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DATA, MediaStore.Audio.Albums.ARTIST, MediaStore.Audio.Media.ALBUM}, null, null,
            "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");

    int count = mCursor.getCount();

    songs = new String[count];
    copyArtist = new String[count];
    albums = new String[count];
    mAudioPath = new String[count];
    artists = new String[count];
    int i = 0;
    if (mCursor.moveToFirst()) {
        do {
            songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
            albums[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
            artists[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
            mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
            copyArtist[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
            final Long albumId = mCursor.getLong(mCursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));

            int duration = mCursor.getInt(mCursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));

            try {
                Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
                Uri uri = ContentUris.withAppendedId(sArtworkUri, albumId);
                uriArr.add(uri);
                ParcelFileDescriptor pfd = this.getContentResolver()
                        .openFileDescriptor(uri, "r");
                FileDescriptor fd = pfd.getFileDescriptor();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 4;
                Bitmap bm = BitmapFactory.decodeFileDescriptor(fd, null, options);
                bitmapArrFull.add(bm);
            } catch (Exception e) {
                e.printStackTrace();
            }
            i++;
        } while (mCursor.moveToNext());
    }
    mCursor.close();

    return songs;
}

If anyone has any ideas, I'd love some help! Thank you so much.


Solution

  • Check out the developer's site, there is useful information :
    http://developer.android.com/training/displaying-bitmaps/index.html