Created
June 8, 2019 09:55
-
-
Save deepakkumardk/7ebddc608473ae91765ddd9ee62a3a1e to your computer and use it in GitHub Desktop.
Extension file to handle images from Google Drive
This file contains hidden or 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
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