Skip to content

Instantly share code, notes, and snippets.

@oianmol
Last active July 28, 2017 12:23
Show Gist options
  • Save oianmol/b03e3deba3bda80f8cd5d8eb5e180970 to your computer and use it in GitHub Desktop.
Save oianmol/b03e3deba3bda80f8cd5d8eb5e180970 to your computer and use it in GitHub Desktop.
Save Uri form gBoard keyboard with uri like gif/png/jpeg's
class UriInfoLoader {
fun loadUriInfo(inputContentInfo: InputContentInfoCompat, context: Context): Single<File>? {
return Single.create<File> { emitter ->
val likUri = inputContentInfo.linkUri
val count = inputContentInfo.description.mimeTypeCount
var ext: String? = null
for (i in 0..count - 1) {
val mimetype = inputContentInfo.description.getMimeType(i)
if (!TextUtils.isEmpty(mimetype)) {
ext = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimetype)
if (!TextUtils.isEmpty(ext)) break
}
}
if (TextUtils.isEmpty(ext)) {
emitter.onError(Exception("Empty"))
}
val contentUri = inputContentInfo.contentUri
try {
val file = Glide.with(context)
.load(contentUri).downloadOnly(com.bumptech.glide.request.target.Target.SIZE_ORIGINAL, com.bumptech.glide.request.target.Target.SIZE_ORIGINAL).get()
var file1 = FileUtils.buildImageKeyboardSupportUri(ext)
var newFilepath = file1.absolutePath
FileUtils.copyFile(file.absolutePath, newFilepath)
emitter.onSuccess(File(newFilepath))
} catch (e: InterruptedException) {
emitter.onError(e)
} catch (e: ExecutionException) {
emitter.onError(e)
} catch (e: Exception) {
emitter.onError(e)
}
}.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment