---
# System prepended metadata

title: Useful function

---

# Useful function

## GetRoundedCornerBitmap
```kotlin=
fun getRoundedCornerBitmap(bitmap: Bitmap, pixels: Int): Bitmap? {
    val output = Bitmap.createBitmap(
        bitmap.width, bitmap
            .height, Bitmap.Config.ARGB_8888
    )
    val canvas = Canvas(output)
    val color = -0xbdbdbe
    val paint = Paint()
    val rect = Rect(0, 0, bitmap.width, bitmap.height)
    val rectF = RectF(rect)
    val roundPx = pixels.toFloat()
    paint.isAntiAlias = true
    canvas.drawARGB(0, 0, 0, 0)
    paint.color = color
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint)
    paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
    canvas.drawBitmap(bitmap, rect, rect, paint)
    return output
}
```

## SaveImageToGallery
```kotlin=
fun saveImageToGallery(context: Context, bmp: Bitmap) { 
// Save image file first
    val appDir = File(Environment.getExternalStorageDirectory(), "domain_name")
    if (!appDir.exists()) {
        appDir.mkdir()
    }
    val fileName = System.currentTimeMillis().toString() + ".jpg"
    val file = File(appDir, fileName)
    try {
        val fos = FileOutputStream(file)
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos)
        fos.flush()
        fos.close()
    } catch (e: FileNotFoundException) {
        e.printStackTrace()
    } catch (e: IOException) {
        e.printStackTrace()
    }
    // Insert image to media
    try {
        MediaStore.Images.Media.insertImage(
            context.contentResolver,
            file.absolutePath, fileName, null
        )
    } catch (e: FileNotFoundException) {
        e.printStackTrace()
    }
    // notify media update
    context.sendBroadcast(
        Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
            Uri.parse("file://${android.R.attr.path}")
        )
    )
}
```