Kotlin bitmapの扱い

個人開発したアプリの宣伝
目的地が設定できる手帳のような使い心地のTODOアプリを公開しています。
Todo with Location

Todo with Location

  • Yoshiko Ichikawa
  • Productivity
  • Free

スポンサードリンク

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
}