有效加载专辑封面Android

时间:2015-08-10 03:42:36

标签: android albumart

我无法有效地加载手机SD卡上的所有专辑封面。我可以完全了解,但由于Bitmap解码过程,我认为可能需要15-20秒。如果可能的话,我也不想丢失订单。这是我的代码:

    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;
}

如果有人有任何想法,我会喜欢一些帮助!非常感谢你。

1 个答案:

答案 0 :(得分:0)

查看开发者网站,有用的信息:
http://developer.android.com/training/displaying-bitmaps/index.html