Reputation: 193
I am trying to get album art uri from music (if null/empty, display my own in app drawable)
but I get:
java.lang.NullPointerException: decodeResource(context.r…rces, R.drawable.ic_logo) must not be null
Utility.kt
fun getAlbumArtBitmap(context: Context, albumId: Long): Bitmap {
try {
return when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.P -> {
val source = ImageDecoder.createSource(
context.contentResolver,
getAlbumArtUri(albumId)
)
ImageDecoder.decodeBitmap(source)
}
else -> MediaStore.Images.Media.getBitmap(
context.contentResolver,
getAlbumArtUri(albumId)
)
}
} catch (e: FileNotFoundException) {
Timber.e(e)
} catch (e: UnsupportedOperationException) {
Timber.e(e)
}
return BitmapFactory.decodeResource(context.resources, R.drawable.ic_logo)
}
The drawable (R.drawable.ic_logo) is an xml vector drawable (svg imported) and sits in drawable folder
Player.kt
val artwork = getAlbumArtBitmap(context, song.albumId)
putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, artwork)
Upvotes: 3
Views: 2015
Reputation: 193
Apparently, R.drawable.ic_logo is a vector image.
I ended up using this:
ContextCompat.getDrawable(context, R.drawable.ic_logo)?.toBitmap()
Converting vector drawable to bitmap, and it worked!
Upvotes: 5