【问题标题】: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 ?: "")
    

    【讨论】:

      猜你喜欢
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      相关资源
      最近更新 更多