Ich möchte die Album-Artworks in meine Musik-App laden und sie in einer Recycleransicht in der Vorschau anzeigen. Ich habe versucht Googles Developer Guide, aber ich lade tatsächlich über 200 Bitmaps, so dass dies nicht funktioniert!AlbumOrtworks ohne OutOfMemoryError laden
Ich habe keine Ahnung wie das geht!
Hier ist meine aktuellen Code:
if (cSong != null && cSong.moveToFirst()) {
do {
int iIDCol = cSong.getColumnIndex(MediaStore.Audio.Media._ID);
int iTitleCol = cSong.getColumnIndex(MediaStore.Audio.Media.TITLE);
int iArtistCol = cSong.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int iAlbumCol = cSong.getColumnIndex(MediaStore.Audio.Media.ALBUM);
int iDurationCol = cSong.getColumnIndex(MediaStore.Audio.Media.DURATION);
int iAlbumIDCol = cSong.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
Bitmap bCover = null;
BitmapFactory.Options bOptions = new BitmapFactory.Options();
bOptions.inJustDecodeBounds = true;
//Throws OutOfMemoryError
/*try {
Uri ArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(ArtworkUri, iAlbumIDCol);
ParcelFileDescriptor pfd = mContext.getContentResolver().openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = pfd.getFileDescriptor();
BitmapFactory.decodeFileDescriptor(fd, null, bOptions);
bOptions.inSampleSize = calculateInSampleSize(bOptions, 100, 100);
bOptions.inJustDecodeBounds = false;
bCover = BitmapFactory.decodeFileDescriptor(fd, null, bOptions);
pfd = null;
fd = null;
}
}
catch (IOException ioe) {
BitmapFactory.decodeResource(mContext.getResources(), R.drawable.standardartwork, bOptions);
bOptions.inSampleSize = calculateInSampleSize(bOptions, 100, 100);
bOptions.inJustDecodeBounds = false;
bCover = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.standardartwork, bOptions);
}*/
SongList.add(new Song(cSong.getLong(iIDCol), cSong.getString(iTitleCol), cSong.getString(iArtistCol), cSong.getString(iAlbumCol), cSong.getInt(iDurationCol), bCover));
}
while (cSong.moveToNext());
Alle meine InSampleSize Methode:
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int size = 1;
if (height > reqHeight && width > reqWidth) {
final int halfHeight = height/2;
final int halfWidth = width/2;
while ((halfHeight/size) > reqHeight && (halfWidth/size) > reqWidth) {
size *= 2;
}
}
return size;
}
Dank!