MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.content.Context
import android.graphics.*
import android.os.Bundle
import android.widget.*
import java.io.ByteArrayOutputStream
import java.io.IOException
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the widgets reference from XML layout
val imageView = findViewById<ImageView>(R.id.imageView)
val imageView2 = findViewById<ImageView>(R.id.imageView2)
// get the bitmap from assets
val bitmap = assetsToBitmap("flower103.jpg")
bitmap?.apply {
// show original bitmap in first image view
imageView.setImageBitmap(this)
// show bitmap to byte array to bitmap in second image view
imageView2.setImageBitmap(toByteArray().toBitmap())
}
}
}
// extension function to get bitmap from assets
fun Context.assetsToBitmap(fileName:String):Bitmap?{
return try {
val stream = assets.open(fileName)
BitmapFactory.decodeStream(stream)
} catch (e: IOException) {
e.printStackTrace()
null
}
}
// extension function to convert bitmap to byte array
fun Bitmap.toByteArray():ByteArray{
ByteArrayOutputStream().apply {
compress(Bitmap.CompressFormat.JPEG,10,this)
return toByteArray()
}
}
// extension function to convert byte array to bitmap
fun ByteArray.toBitmap():Bitmap{
return BitmapFactory.decodeByteArray(this,0,size)
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
android:background="#DCDCDC"
android:padding="24dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="250dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Original Bitmap"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="250dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bitmap To ByteArray To Bitmap"
app:layout_constraintEnd_toEndOf="@+id/imageView2"
app:layout_constraintStart_toStartOf="@+id/imageView2"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
</androidx.constraintlayout.widget.ConstraintLayout>

- android kotlin - ThumbnailUtils example
- android kotlin - Base64 string to bitmap
- android kotlin - Bitmap to base64 string
- android kotlin - Bitmap to grayscale
- android kotlin - CalendarView get selected date
- android kotlin - CalendarView set date
- android kotlin - CalendarView example
- android kotlin - Vertical progress bar example
- android kotlin - Indeterminate ProgressBar example
- android kotlin - Horizontal ProgressBar programmatically
- android kotlin - ListView add item programmatically
- android kotlin - ListView add header programmatically
- android kotlin - ListView OnItemClickListener example
- android kotlin - AlertDialog title align center programmatically
- android kotlin - AlertDialog title text color size bold programmatically