高效加载专辑封面 Android
Loading album art efficiently Android
我一直无法有效加载位于 phone 的 SD 卡上的所有专辑封面。我可以全部搞定,但我相信这可能需要 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;
}
如果有人有任何想法,我很乐意提供帮助!非常感谢。
我一直无法有效加载位于 phone 的 SD 卡上的所有专辑封面。我可以全部搞定,但我相信这可能需要 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;
}
如果有人有任何想法,我很乐意提供帮助!非常感谢。