Last active
October 11, 2021 11:57
-
-
Save HosseinKurd/438d7711e8856daa21cf5cf7b1f55cdb to your computer and use it in GitHub Desktop.
Kotlin Uri To File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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