mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-03-24 07:54:42 +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.Bitmap
|
||||||
import android.graphics.BitmapFactory
|
import android.graphics.BitmapFactory
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import java.io.InputStream
|
||||||
|
|
||||||
fun Bitmap.aspectRatio() = width / height.toFloat()
|
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(
|
private fun loadImageBounds(
|
||||||
context: Context, uri: Uri
|
context: Context, uri: Uri
|
||||||
): BitmapFactory.Options {
|
): BitmapFactory.Options {
|
||||||
context.contentResolver.openInputStream(uri)!!.use {
|
openUriInputStream(context, uri)!!.use {
|
||||||
val opt = BitmapFactory.Options().apply {
|
val opt = BitmapFactory.Options().apply {
|
||||||
inJustDecodeBounds = true
|
inJustDecodeBounds = true
|
||||||
}
|
}
|
||||||
|
@ -128,7 +139,7 @@ interface BitmapUtil {
|
||||||
private fun loadImage(
|
private fun loadImage(
|
||||||
context: Context, uri: Uri, opt: BitmapFactory.Options
|
context: Context, uri: Uri, opt: BitmapFactory.Options
|
||||||
): Bitmap {
|
): Bitmap {
|
||||||
context.contentResolver.openInputStream(uri)!!.use {
|
openUriInputStream(context, uri)!!.use {
|
||||||
return BitmapFactory.decodeStream(it, null, opt)!!
|
return BitmapFactory.decodeStream(it, null, opt)!!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,10 +31,15 @@ class ContentUriChannelHandler(context: Context) :
|
||||||
private fun readUri(uri: String, result: MethodChannel.Result) {
|
private fun readUri(uri: String, result: MethodChannel.Result) {
|
||||||
val uriTyped = Uri.parse(uri)
|
val uriTyped = Uri.parse(uri)
|
||||||
try {
|
try {
|
||||||
val bytes =
|
val bytes = if (UriUtil.isAssetUri(uriTyped)) {
|
||||||
|
context.assets.open(UriUtil.getAssetUriPath(uriTyped)).use {
|
||||||
|
it.readBytes()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
context.contentResolver.openInputStream(uriTyped)!!.use {
|
context.contentResolver.openInputStream(uriTyped)!!.use {
|
||||||
it.readBytes()
|
it.readBytes()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
result.success(bytes)
|
result.success(bytes)
|
||||||
} catch (e: FileNotFoundException) {
|
} catch (e: FileNotFoundException) {
|
||||||
result.error("fileNotFoundException", e.toString(), null)
|
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"
|
private const val TAG = "UriUtil"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue