Skip to content

Instantly share code, notes, and snippets.

@deepakkumardk
Created June 8, 2019 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepakkumardk/7ebddc608473ae91765ddd9ee62a3a1e to your computer and use it in GitHub Desktop.
Save deepakkumardk/7ebddc608473ae91765ddd9ee62a3a1e to your computer and use it in GitHub Desktop.
Extension file to handle images from Google Drive
fun Activity?.createTempFile(uri: Uri, callback: DriveTempFileCallback) {
val startTime = System.currentTimeMillis() // Just for debugging purpose
this.doAsyncResult { // Here I have used the Anko library to simplify things
var stream: InputStream? = null
try {
stream = this@createTempFile?.contentResolver?.openInputStream(uri)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
val tempFile = File.createTempFile(Date().time.toString(), ".JPEG")
tempFile.deleteOnExit()
try {
IOUtils.copy(stream, FileOutputStream(tempFile))
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
onComplete {
val fetchingTime = System.currentTimeMillis() - startTime
log("Fetching time of file $fetchingTime")
callback.onFileCreated(tempFile)
return@onComplete
}
return@doAsyncResult tempFile
}
}
fun Activity?.getUriListFromTempFile(uriList: ArrayList<Uri>, callback: DriveImageCallback) {
val startTime = System.currentTimeMillis()
val resultList = arrayListOf<Uri>()
for (uri in uriList) {
createTempFile(uri, DriveTempFileCallback {
resultList.add(Uri.fromFile(it))
if (resultList.size == uriList.size) {
callback.onSuccess(resultList)
val fetchingTime = System.currentTimeMillis() - startTime
log("Fetching time of all files $fetchingTime")
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment