0

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; } 
3
  • have you tried Glide? Commented Oct 22, 2016 at 13:24
  • I haven't. I'm using Universal Image Loader in this app to load other images. but as you can see im getting the bitmap from audio stream and setting it to imageview using setImageBitmap method only. Commented Oct 22, 2016 at 13:26
  • Use Fresco image loading library by Facebook. It automatically reduces all the OOM issues by recycling all the images and clearing memory on its own. In my own experience, UIL does cause lots of OOM issues. Use Fresco and you will be happy :P Commented Oct 22, 2016 at 13:28

1 Answer 1

1

You need to use a library to load the images.

Glide is an amazing library to load the images.

First of all the dependency is

put these two in your build.gradle

compile 'com.github.bumptech.glide:glide:3.7.0' 

and then in your adapter where your loading the image all you neeed to do is:

Glide .with(context) .load(AudioArt) .into(yourImageView); 

You can further customize it to your needs too, Just visit their Github Page.

Future Studio also have great tutorial on customizing it.

Sign up to request clarification or add additional context in comments.

2 Comments

is it getting the bitmap by audio stream?? i dont think??
It does. Variable AudioArt can be byte[]. Good luck!)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.