Search code examples
javaandroidbitmapremoteview

Bitmap in notification remoteviews cannot recycle


I want to write mp3 player app. show music album on notification

Here is the code:

mCoverBitmap = MusicModel.getAlbumArt(musicItem.getUri());
    if (mCoverBitmap != null) {
        mNormalRemoteViews.setImageViewBitmap(R.id.notification_image, mCoverBitmap);
    } else {
        mNormalRemoteViews.setImageViewResource(R.id.notification_image, R.drawable.default_album);
    }

but it leads to out of memory after I change a lot of music. So I recycle the mCoverBitmap. But it crashed. java.lang.IllegalStateException: Can't parcel a recycled bitmap

Then I try to cache the bitmap. And recycle it the next time we need a notification.

    BitmapUtil.recycleBitmap(mCoverCache);
    mCoverCache = mCoverBitmap;
    mCoverBitmap = MusicModel.getAlbumArt(musicItem.getUri());
    if (mCoverBitmap != null) {
        mNormalRemoteViews.setImageViewBitmap(R.id.notification_image, mCoverBitmap);
    } else {
        mNormalRemoteViews.setImageViewResource(R.id.notification_image, R.drawable.default_album);
    }

But it leads to the same exception. how to recycle the bitmap?


Solution

  • Re using the same remoteview for your application wil cause the out of memory error. So instead of recycling just recreate the remoteviews. https://groups.google.com/forum/m/#!topic/android-developers/qQ4SV5wL7uM