【问题标题】:Android: Display image from storage in webView using HTMLAndroid:使用 HTML 在 webView 中显示存储中的图像
【发布时间】:2018-04-24 17:19:32
【问题描述】:
我有一个显示 html 的 webview,我有一个不想显示的 URI 图像,这是我的代码:
<td class="title">
<img src="#LOGO#" style="width:15%; max-width:300px;"/>
</td>
这是我的代码:
str = str.replace("#LOGO#", Uri.parse(myStringPath).toString())
图片不显示
【问题讨论】:
标签:
android
webview
kotlin
【解决方案1】:
您必须将图像转换为位图,然后您才能在 Html 中显示位图:
var logoImage: String? = null
try {
val imageUri = Uri.parse(myStringPath)
var imageStream: InputStream?
imageStream = this.contentResolver.openInputStream(imageUri)
val selectedImage = BitmapFactory.decodeStream(imageStream)
// Convert bitmap to Base64 encoded image for web
val byteArrayOutputStream = ByteArrayOutputStream()
selectedImage.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream)
val byteArray = byteArrayOutputStream.toByteArray()
val imageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT)
logoImage = "data:image/png;base64,$imageBase64"
} catch (e: FileNotFoundException) {
println("Error: ${e.localizedMessage}, ${e.message}")
}
str = str.replace("#LOGO#", logoImage ?: "")