0
fun resize(x: Bitmap, max: Int): Bitmap {

    var width = x.width
    var height = x.height

    val scale: Double = width.toDouble() / height.toDouble()

    if (scale > 1) {
        width = max
        var newHeight = width / scale
        height = newHeight.toInt()
    } else {
        height = max
        var newWidth = height * scale
        width = newWidth.toInt()

    }
    return Bitmap.createScaledBitmap(x, width / 2, height / 2, true)

}

This is the code to execute it

        val smallBitmap = small(bitmap!!, 300)
        val outputStream = ByteArrayOutputStream()
        smallBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
        val byteArray = outputStream.toByteArray()

I have 2 fragments. And I am picking photo from the gallery and pass it to other fragment. And when I send it the photos come out too pixelated and blurry. It's because of resize and scaling. But I do not know what to do.

Mike
  • 2,547
  • 3
  • 16
  • 30
  • 1
    `createScaledBitmap` uses either bilinear filtering or nearest-neighbor scaling, so it might not give great-looking results a lot of the time. – Michael Dec 08 '21 at 10:00
  • there's a better option and it's not too hard to implement. Try this answer: https://stackoverflow.com/a/7468636/9008140 – John Lord Dec 08 '21 at 22:16

0 Answers0