4

I'm trying to get the large icon for the notification in android app like that:

val bm = BitmapFactory.decodeResource(this.resources, R.mipmap.ic_launcher)

It works correctly in android 7.1, but throws exception in android 8:

Caused by: java.lang.IllegalStateException: bm must not be null

It is the same code, nothing changes. Does someone know whats wrong? I'm using Kotlin(if it matters).

fr3ak
  • 493
  • 3
  • 16
  • It´s because `val` can´t get values from object which can be set to null. You need to wrap it on optional so there is every time something present but maybe with no inner value. Or just use `var bm: ?` - the `?` does it make to allow NULL references. – LenglBoy Apr 04 '18 at 10:44
  • the problem is not that `bm` could be `null`, its because on android 7.1 is not and in android 8 it is `null` – fr3ak Apr 04 '18 at 10:48
  • Do you have `mipmap-anydpi-v26` in your `res` directory? – Artyom Apr 04 '18 at 10:50
  • yes, I have `mipmap-anydpi-v26 `, but it has only xml-s – fr3ak Apr 04 '18 at 10:58

1 Answers1

3

It seems that R.mipmap.ic_launcher is not an id of BitmapDrawable resource.
ic_launcher.xml file from mipmap-anydpi-v26 directory is used on Android 8 instead of ic_launcher.png from mipmap-*dpi. It contains adaptive icon resource.

You may try this solution to convert it to a Bitmap.

Artyom
  • 1,165
  • 14
  • 22