Skip to content Skip to sidebar Skip to footer

Android Studio (kotlin) Save A Given Image In Given Path In Gallery 2020

In Android Studio I want to save a BitMap to a specific folder in the galery of the android device for example /test_pictures as an image. The easy ways I found on the internet se

Solution 1:

Kotlin bitmap extension like this:

fun Bitmap.saveImage(context: Context): Uri? {
if (android.os.Build.VERSION.SDK_INT >= 29) {
    val values = ContentValues()
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
    values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000)
    values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis())
    values.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/test_pictures")
    values.put(MediaStore.Images.Media.IS_PENDING, true)
    values.put(MediaStore.Images.Media.DISPLAY_NAME, "img_${SystemClock.uptimeMillis()}")

    val uri: Uri? =
        context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
    if (uri != null) {
        saveImageToStream(this, context.contentResolver.openOutputStream(uri))
        values.put(MediaStore.Images.Media.IS_PENDING, false)
        context.contentResolver.update(uri, values, null, null)
        return uri
    }
} else {
    val directory =
        File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).toString() + separator + "test_pictures")
    if (!directory.exists()) {
        directory.mkdirs()
    }
    val fileName =  "img_${SystemClock.uptimeMillis()}"+ ".jpeg"val file = File(directory, fileName)
    saveImageToStream(this, FileOutputStream(file))
    if (file.absolutePath != null) {
        val values = contentValues()
        values.put(MediaStore.Images.Media.DATA, file.absolutePath)
        // .DATA is deprecated in API 29
        context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
        return Uri.fromFile(file)
    }
}
returnnull
}


funsaveImageToStream(bitmap: Bitmap, outputStream: OutputStream?) {
if (outputStream != null) {
    try {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
        outputStream.close()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}
}

val imageUri = YourBitmap.saveImage(applicationContext)

Post a Comment for "Android Studio (kotlin) Save A Given Image In Given Path In Gallery 2020"