반응형
private fun resizeBitMap(uri: Uri, resize: Int): Bitmap? {
    var resizeBitmap: Bitmap? = null
    val ratioTemp = 2

    val options = BitmapFactory.Options()
    try {
        BitmapFactory.decodeStream(
            applicationContext.contentResolver.openInputStream(uri),
            null,
            options
        )
        var width = options.outWidth
        var height = options.outHeight
        var sampleSize = 1

        while (true) {
            if (width / ratioTemp < resize || height / ratioTemp < resize) break

            width /= ratioTemp
            height /= ratioTemp
            sampleSize *= ratioTemp
        }

        options.inSampleSize = sampleSize
        val bitmap = BitmapFactory.decodeStream(
            applicationContext.contentResolver.openInputStream(uri),
            null,
            options
        )
        resizeBitmap = bitmap

    } catch (e: FileNotFoundException) {
        e.printStackTrace()
    }

    return resizeBitmap
}
반응형

+ Recent posts