Skip to content

Instantly share code, notes, and snippets.

@root-ansh
Created September 7, 2025 10:48
Show Gist options
  • Save root-ansh/473ff1cdf8a329d6050143cfcc7fb823 to your computer and use it in GitHub Desktop.
Save root-ansh/473ff1cdf8a329d6050143cfcc7fb823 to your computer and use it in GitHub Desktop.
@Throws
suspend fun Context.saveFileToUserSelectedPath(uri: Uri, sourceFile: File): Uri {
return withContext(Dispatchers.IO) {
contentResolver.openOutputStream(uri)?.use { outputStream ->
sourceFile.inputStream().use { inputStream ->
inputStream.copyTo(outputStream)
}
}
uri
}
}
suspend fun Context.saveBitmapToUserSelectedPath(uri: Uri, bitmap: Bitmap): Uri {
return withContext(Dispatchers.IO){
contentResolver.openOutputStream(uri)?.use { outputStream ->
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
outputStream.flush()
}
uri
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment