Kotlin bitmapの扱い


ByteからBitmap

public fun ByteToBitmap(bytes:ByteArray):Bitmap{
    val opt = BitmapFactory.Options()
    opt.inJustDecodeBounds = false
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.size, opt)
}

//ImageViewに画像をセット
ImageView.setImageBitmap( ByteToBitmap(byteArray) )

BitmapからByte

public fun BitmapToByte(bmp:Bitmap):ByteArray{
    val stream = ByteArrayOutputStream()
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream)
    return stream.toByteArray()
}

streamからBitmap

var inputStream = activity!!.contentResolver.openInputStream(uri)

public fun StreamToBitmap(inputStream:InputStream):Bitmap{
    val opt = BitmapFactory.Options()
    opt.inJustDecodeBounds = false
    BitmapFactory.decodeStream(inputStream, null, opt)
    inputStream.close()
    return bmp
}