HBiSoft / HBRecorder

Lightweight screen recording Android library
MIT License
423 stars 141 forks source link

Notification icon causes crash #100

Closed JJdeGroot closed 2 years ago

JJdeGroot commented 2 years ago

Describe the bug When starting the screen recorder, it immediately crashes when loading the default notification icon.

Somehow the Demo app does work. The only difference I can find is that it sets the notification icon with a byte array. When I do this, my own app still crashes. Other than changing the notification settings, everything is set to default.

Log

android.app.RemoteServiceException: Bad notification posted from package nl.ontoegankelijk: Couldn't create icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=nl.ontoegankelijk id=0x7f0f0000) visible user=0 )
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1517)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Can it be reproduced in demo app No, it works in the demo app. The demo app does not show a notification. It does show the casting icon.

HBRecorder version 2.0.2

Device information

Screenshots Not applicable

JJdeGroot commented 2 years ago

The app also crashes on Lenovo P2a42 with Android 7 / API 24

android.app.RemoteServiceException: Bad notification posted from package nl.ontoegankelijk: Couldn't create icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=nl.ontoegankelijk id=0x7f0f0000) visible user=0 )
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1637)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6111)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
HBiSoft commented 2 years ago

Please show me how you set the notification icon.

JJdeGroot commented 2 years ago

Hi, I think this one is actually caused by the system and not by the HBRecorder library. I did some research and found out it can be caused by certain Android launchers. I did not have a .png alternative for my app icon. When I added a .png app icon the crash no longer happened.

Here is some of the code.

private fun getBitmapFromVectorDrawable(context: Context, drawableId: Int): Bitmap? {
        ContextCompat.getDrawable(context, drawableId)?.let { drawable ->
            val bitmap = Bitmap.createBitmap(
                drawable.intrinsicWidth,
                drawable.intrinsicHeight, Bitmap.Config.ARGB_8888
            )
            val canvas = Canvas(bitmap)
            drawable.setBounds(0, 0, canvas.width, canvas.height)
            drawable.draw(canvas)

            return bitmap
        }
        return null
    }

    private fun getByteArrayFromBitmap(bitmap: Bitmap): ByteArray {
        val stream = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
        return stream.toByteArray()
    }

    override fun onViewCreated() {
        super.onViewCreated()

        recorder = HBRecorder(this, this)
        recorder?.setNotificationTitle(getString(R.string.recording_notification_title))
        recorder?.setNotificationDescription(getString(R.string.recording_notification_message))
        recorder?.setNotificationButtonText(getString(R.string.recording_notification_action))

        getBitmapFromVectorDrawable(this, R.drawable.icon_notification)?.let { bitmap ->
            val bytes = getByteArrayFromBitmap(bitmap)
            recorder?.setNotificationSmallIcon(bytes)
        }
}
HBiSoft commented 2 years ago

Ok, closing. I will reply on your other issue.