0

When viewing my media notification the LargeIcon of the albumart is very pixelated. However Samsung Music's notification of the same albumart is of the correct resolution.

I've attached 4 images below of what this looks like, both in the notification and the player view.

Here's the relevant code that creates the notification and converts the albumart uri into a bitmap. The commented out parts are other ways I tried to fix this issue. Setting the largeicon as metadata instead of as a largeicon just results in no albumart in the notification. scaling the bitmap doesn't change how pixelated it is, just where it's centered. I have an image called test.png in the drawable folder that when I use this as the largeIcon it looks perfect without any pixelation.

private fun buildNotification(song: Song, isPlaying: Boolean): Notification {
        val playPauseAction = if (isPlaying) {
            NotificationCompat.Action(
                R.drawable.ic_pause, "Pause",
                MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PAUSE)
            )
        } else {
            NotificationCompat.Action(
                R.drawable.ic_play_arrow, "Play",
                MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY)
            )
        }

        val style = androidx.media.app.NotificationCompat.MediaStyle()
            .setShowActionsInCompactView(0)
            .setMediaSession(mediaSession.sessionToken)

//        val metadataBuilder = MediaMetadataCompat.Builder()
//            .putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.songName)
//            .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.songArtist)
//            .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, getBitmapFromUri(song.songCover)) // used as largeIcon
//
//        mediaSession.setMetadata(metadataBuilder.build())

        return NotificationCompat.Builder(this, "shufflo_music_channel")
            .setContentTitle(song.songName)
            .setContentText(song.songArtist)
            .setLargeIcon(getBitmapFromUri(song.songCover))
            //.setLargeIcon(
            //    BitmapFactory.decodeResource(getResources(), R.drawable.test).scale(128, 128, false))
            .setSmallIcon(R.drawable.ic_album_art_placeholder)
            .addAction(playPauseAction)
            .setStyle(style)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setOnlyAlertOnce(true)
            .setOngoing(isPlaying) // important for making it non-dismissible
            .build()
    }

    private fun getBitmapFromUri(uri: Uri): Bitmap? {
        return try {
            val inputStream = contentResolver.openInputStream(uri)
            val original = BitmapFactory.decodeStream(inputStream)
            inputStream?.close()
            original
            //original?.scale(128, 128, false)
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }

What my notification looks like - pixelated album art

What Samsung Music's notification looks like - original album art size with high-resolution

What my player looks like - the original URI is of great quality here but not in the notification

What Samsung Music's player looks like - the album art is high quality here as well

I tried the 3 commented out methods in the above code, of which nothing helped except showing the test.png in the notification, which works resolution wise but won't be the correct image. I am expecting the notification's large icon to look identical to Samsung Music's or really any other media app's notification. What resulted in my case is a pixelated large icon no matter what song is being played, no matter the resolution of the albumart.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.