将base64转换为图像无法正常工作

时间:2018-11-17 08:53:21

标签: android kotlin base64 android-imageview

我正在将base64转换为图像,但是当我将其设置为imageView时,它什么也没显示。

我已经尝试了几乎所有可用于stackoverflow的解决方案,但结果相同。这就是我现在正在尝试的方式

var iconBase64String = "myBASE64StringWhichIsGivenBelow"
// remove 'data:image/svg+xml;base64,' from start of string
iconBase64String = iconBase64String.substring(iconBase64String.indexOf(",")  + 1)
val image = findViewById<ImageView>(R.id.imageView)

val imageAsBytes = Base64.decode(iconBase64String.toByteArray(), Base64.DEFAULT)
val decodedString = Base64.decode(iconBase64String, Base64.DEFAULT)
val bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)
image.setImageBitmap(bitmap)

我也尝试过使用glide

var iconBase64String = "myBASE64StringWhichIsGivenBelow"
// remove 'data:image/svg+xml;base64,' from start of string
iconBase64String = iconBase64String.substring(iconBase64String.indexOf(",")  + 1)
val image = findViewById<ImageView>(R.id.imageView)

val imageAsBytes = Base64.decode(iconBase64String.toByteArray(), Base64.DEFAULT)
Glide.with(this)
      .asBitmap()
      .load(imageAsBytes)
      .into(image)

这是我要转换的base64:

  

数据:图像/ SVG + xml的; BASE64,PHN2ZyBoZWlnaHQ9IjU0IiB2aWV3Qm94PSIwIDAgNDIuMTE3ODQgNDIuMTI5NDQ2IiB3aWR0aD0iNTQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPjxjbGlwUGF0aCBpZD0iYSI + PHBhdGggZD0ibTcyMC42IDMwNi40aDUwOC43djI2NmgtNTA4Ljd6IiBoZWlnaHQ9IjEwMCUiIHRyYW5zZm9ybT0iIiB3aWR0aD0iMTAwJSIvPjwvY2xpcFBhdGg + PGNsaXBQYXRoIGlkPSJiIj48cGF0aCBkPSJtNzIwLjYgMGgyNTQuNHY1NzIuNGgtMjU0LjR6IiBoZWlnaHQ9IjEw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wODM3OTkgLTI4LjUxMDY2MiAwKSIvPjxwYXRoIGNsaXAtcGF0aD0idXJsKCNkKSIgZD0ibTcyMC42IDQ3MC4zIDI1NC40IDM1OC40di0yMDguMXoiIG9wYWNpdHk9Ii40NSIgdHJhbnNmb3JtPSJtYXRyaXgoLjA1MDgzNzk5IDAgMCAuMDUwODM3OTkgLTI4LjUxMDY2MiAwKSIvPjxwYXRoIGNsaXAtcGF0aD0idXJsKCNlKSIgZD0ibTk3NSA2MjAuNnYyMDguMWwyNTQuNS0zNTguNHoiIG9wYWNpdHk9Ii44IiB0cmFuc2Zvcm09Im1hdHJpeCguMDUwODM3OTkgMCAwIC4wNTA4Mzc5OSAtMjguNTEwNjYyIDApIi8 + PC9nPjwvc3ZnPg ==

1 个答案:

答案 0 :(得分:0)

您可以使用滑行显示图片

          String photoId="base64value"
          Glide.with(getApplicationContext())
            .load(photoId)
            .apply(RequestOptions.circleCropTransform())  //If you get circule
            .into(employeeImage); //xml file name