I am developing an android app and I have to deal with a lot of bitmaps there. Sometimes the app crashes due to out of memory.
So I have a custom object(Song class) and there I have fields like title, artist, link and Bitmap. I'm getting the album cover from the audio stream and assign that bitmap into relevant object's field.
Here is the code for getting album cover bitmap,
public static CustomBitmap downloadAudioCover(Context context, final String url) { final MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever(); try { metaRetriever.setDataSource(url, new HashMap<String, String>()); final byte[] art = metaRetriever.getEmbeddedPicture(); int width = context.getResources().getDimensionPixelSize(R.dimen.image_audio_cover_width); int height = context.getResources().getDimensionPixelSize(R.dimen.image_audio_cover_height); CustomBitmap customBitmap = new CustomBitmap(); customBitmap.setBitmap(decodeSampledBitmapFromResource(art, width, height)); return customBitmap; } catch (Exception e) { return null; } } Here I have used a CustomBitmap class to Serialize the object and save in a file so I can avoid the above process again and next time when the user comes to the app user can see the album cover without any delay.
The issue is I am getting roughly 50 songs which means I need to hold 50 objects with 50 bitmaps.
For display this data I am using a RecyclerView.
Since I don't have the bitmaps initially, I need to call that above method for every single song. So I get the songs first(without the bitmap) and after I fetching all the songs, I loop through the array list to get the bitmaps. Once I got all the bitmaps I call notifyDataSetChanged method on the adapter.
So this process is more memory intensive. I need a better and efficient way to handle this list of bitmaps in my app. Any suggestions would be appreciated.
* Is getting the bitmap inside onBindViewHolder and setting to the imageview without assigning to object a good solution for this?
Thanks.
Note : this is the code to sampling bitmaps
private static Bitmap decodeSampledBitmapFromResource(byte[] arr, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(arr, 0, arr.length, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeByteArray(arr, 0, arr.length, options); } private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) { inSampleSize *= 2; } } return inSampleSize; }