diff --git a/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/BitmapUtil.kt b/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/BitmapUtil.kt index b6c67475..9edfddf1 100644 --- a/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/BitmapUtil.kt +++ b/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/BitmapUtil.kt @@ -4,6 +4,7 @@ import android.content.Context import android.graphics.Bitmap import android.graphics.BitmapFactory import android.net.Uri +import java.io.InputStream fun Bitmap.aspectRatio() = width / height.toFloat() @@ -113,10 +114,20 @@ interface BitmapUtil { } } + private fun openUriInputStream( + context: Context, uri: Uri + ): InputStream? { + return if (UriUtil.isAssetUri(uri)) { + context.assets.open(UriUtil.getAssetUriPath(uri)) + } else { + context.contentResolver.openInputStream(uri) + } + } + private fun loadImageBounds( context: Context, uri: Uri ): BitmapFactory.Options { - context.contentResolver.openInputStream(uri)!!.use { + openUriInputStream(context, uri)!!.use { val opt = BitmapFactory.Options().apply { inJustDecodeBounds = true } @@ -128,7 +139,7 @@ interface BitmapUtil { private fun loadImage( context: Context, uri: Uri, opt: BitmapFactory.Options ): Bitmap { - context.contentResolver.openInputStream(uri)!!.use { + openUriInputStream(context, uri)!!.use { return BitmapFactory.decodeStream(it, null, opt)!! } } diff --git a/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/ContentUriChannelHandler.kt b/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/ContentUriChannelHandler.kt index 4fd0e123..f04c0107 100644 --- a/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/ContentUriChannelHandler.kt +++ b/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/ContentUriChannelHandler.kt @@ -31,10 +31,15 @@ class ContentUriChannelHandler(context: Context) : private fun readUri(uri: String, result: MethodChannel.Result) { val uriTyped = Uri.parse(uri) try { - val bytes = + val bytes = if (UriUtil.isAssetUri(uriTyped)) { + context.assets.open(UriUtil.getAssetUriPath(uriTyped)).use { + it.readBytes() + } + } else { context.contentResolver.openInputStream(uriTyped)!!.use { it.readBytes() } + } result.success(bytes) } catch (e: FileNotFoundException) { result.error("fileNotFoundException", e.toString(), null) diff --git a/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/UriUtil.kt b/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/UriUtil.kt index 9b0df237..f0d2d353 100644 --- a/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/UriUtil.kt +++ b/plugin/android/src/main/kotlin/com/nkming/nc_photos/plugin/UriUtil.kt @@ -28,6 +28,24 @@ interface UriUtil { } } + /** + * Asset URI is a non-standard Uri that points to an asset file. + * + * An asset URI is formatted as file:///android_asset/path/to/file + * + * @param uri + * @return + */ + fun isAssetUri(uri: Uri): Boolean { + return uri.scheme == "file" && uri.path?.startsWith( + "/android_asset/" + ) == true + } + + fun getAssetUriPath(uri: Uri): String { + return uri.path!!.substring("/android_asset/".length) + } + private const val TAG = "UriUtil" } }