Skip to content

Instantly share code, notes, and snippets.

@tomoima525
Created September 9, 2018 04:49
Show Gist options
  • Save tomoima525/5f31e405ff0860e85516eb8615915498 to your computer and use it in GitHub Desktop.
Save tomoima525/5f31e405ff0860e85516eb8615915498 to your computer and use it in GitHub Desktop.
@ReactMethod
fun generate(imagePath: String,
renderSettings: ReadableArray,
width: Int,
height: Int,
promise: Promise
) {
Log.d("Thumbnail", "====== called $renderSettings $imagePath")
launch(CommonPool) {
try {
val uri = Uri.parse(imagePath)
val projection = arrayOf(MediaStore.Images.Media._ID)
var imageId: Long? = null
Log.d("Thumbnail", "===== image $uri path $imagePath")
// associate Looper
Looper.prepare()
val cursorLoader = CursorLoader(
reactApplicationContext,
uri, projection, null, null, null)
cursorLoader.loadInBackground()?.apply {
imageId = async {
use {
val columnIndex =
getColumnIndexOrThrow(MediaStore.Images.Media._ID)
moveToFirst()
return@async getLong(columnIndex)
}
}.await()
}
Log.d("Thumbnail", "===== imageId $imageId")
if (imageId == null) {
Log.d("Thumbnail", "===== No thumbnail")
promise.reject(IllegalAccessException("No thumbnail"))
return@launch
}
val miniThumbCursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(
reactApplicationContext.contentResolver,
imageId!!,
MediaStore.Images.Thumbnails.MINI_KIND,
null)
if (miniThumbCursor != null && miniThumbCursor.count > 0) {
Log.d("Thumbnail", "===== ok")
miniThumbCursor.moveToFirst()
val result = miniThumbCursor.getString(
miniThumbCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA))
val map = Arguments.createMap()
map.putString("path", imagePath)
map.putString("thumbnail", result)
promise.resolve(map)
miniThumbCursor.close()
}
Log.d("Thumbnail", "===== end")
} catch (e: Exception) {
promise.reject(e)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment