Skip to content

Instantly share code, notes, and snippets.

@HosseinKurd
Last active October 11, 2021 11:57
Show Gist options
  • Save HosseinKurd/438d7711e8856daa21cf5cf7b1f55cdb to your computer and use it in GitHub Desktop.
Save HosseinKurd/438d7711e8856daa21cf5cf7b1f55cdb to your computer and use it in GitHub Desktop.
Kotlin Uri To File
private fun decreaseImage(context: Context?, uri: Uri) {
// println("$TAG , decreaseImage ...")
context?.let {
// println("$TAG , decreaseImage ${uri.path}")
val byteArray =
IOUtils.toByteArray(it.contentResolver.openInputStream(uri))
// println("$TAG , decreaseImage ${byteArray.size}")
val filename = (uri.lastPathSegment ?: "file_${SystemClock.elapsedRealtime()}")
.replace(":", "_")
.replace("/", "_")
val file: File = File(
it.cacheDir.absolutePath,
filename
)
.apply {
writeBytes(byteArray)
}
// Do what ever you wanna
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment