mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-01-22 16:56:19 +01:00
Support the special asset uri syntax
This commit is contained in:
parent
e49f06e7bc
commit
74b69432c6
3 changed files with 37 additions and 3 deletions
|
@ -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)!!
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue