0

I am converting base64 to image but when I set it to imageView it shows nothing.

I have tried almost every solution available on stackoverflow but same result. This is how I am trying to do it right now

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)

I have also tried with 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)

here is base64 which i am trying to convert:

data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjU0IiB2aWV3Qm94PSIwIDAgNDIuMTE3ODQgNDIuMTI5NDQ2IiB3aWR0aD0iNTQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPjxjbGlwUGF0aCBpZD0iYSI+PHBhdGggZD0ibTcyMC42IDMwNi40aDUwOC43djI2NmgtNTA4Ljd6IiBoZWlnaHQ9IjEwMCUiIHRyYW5zZm9ybT0iIiB3aWR0aD0iMTAwJSIvPjwvY2xpcFBhdGg+PGNsaXBQYXRoIGlkPSJiIj48cGF0aCBkPSJtNzIwLjYgMGgyNTQuNHY1NzIuNGgtMjU0LjR6IiBoZWlnaHQ9IjEwMCUiIHRyYW5zZm9ybT0iIiB3aWR0aD0iMTAwJSIvPjwvY2xpcFBhdGg+PGNsaXBQYXRoIGlkPSJj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MC4zIDI1NC40IDM1OC40di0yMDguMXoiIG9wYWNpdHk9Ii40NSIgdHJhbnNmb3JtPSJtYXRyaXgoLjA1MDgzNzk5IDAgMCAuMDUwODM3OTkgLTI4LjUxMDY2MiAwKSIvPjxwYXRoIGNsaXAtcGF0aD0idXJsKCNlKSIgZD0ibTk3NSA2MjAuNnYyMDguMWwyNTQuNS0zNTguNHoiIG9wYWNpdHk9Ii44IiB0cmFuc2Zvcm09Im1hdHJpeCguMDUwODM3OTkgMCAwIC4wNTA4Mzc5OSAtMjguNTEwNjYyIDApIi8+PC9nPjwvc3ZnPg==

Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
Zohab Ali
  • 8,426
  • 4
  • 55
  • 63
  • That decodes to an SVG (provided you're correctly stripping that plaintext header junk first) – https://drive.google.com/file/d/1h9NCT4mGJ7q5G3iaCjCiRVNIRtBd5WXR/view?usp=drivesdk. `BitmapFactory` can't do anything with that. – Mike M. Nov 17 '18 at 08:56
  • @MikeM. so what should I do? if I dnt strip that header junk it gives me error while decoding. In short how can I make it work? – Zohab Ali Nov 17 '18 at 08:59
  • Oh, never mind. I see where you're stripping that. Anyhoo, it's an SVG. As mentioned, `BitmapFactory` can't handle that, so it'll just return null. – Mike M. Nov 17 '18 at 09:00
  • @MikeM. so you are saying I can never show that image? – Zohab Ali Nov 17 '18 at 09:04
  • Also, whoops. I didn't realize Drive would actually render that. I meant for you to see the text. In any case, I just recently saw a trusted fellow user recommend [this particular custom `ImageView`](https://bigbadaboom.github.io/androidsvg/doc/index.html?com/caverock/androidsvg/SVGImageView.html) to display those. I can't vouch for it myself, as I've not used it yet. – Mike M. Nov 17 '18 at 09:05
  • 1
    Here ya go: https://drive.google.com/file/d/1corT23o1SnXmkYDCSsYfGeqx2JF9ReKX/view?usp=drivesdk. That's what that decodes to. – Mike M. Nov 17 '18 at 09:07
  • 1
    thanks man! let me test that `svg imageview` solution. it seems like it should work – Zohab Ali Nov 17 '18 at 09:09
  • I was just doing a little investigating, 'cause I didn't realize Glide could handle Base64 directly, and I came upon this post: https://stackoverflow.com/a/35508121. Seems you might be able to use Glide together with a few classes from that custom `View` repo to load it into a regular `ImageView`, if you'd prefer to do that. I'm only speculating, here. I can't test anything at the moment. Just an FYI. – Mike M. Nov 17 '18 at 09:25

1 Answers1

0

You can use glide for show Image

          String photoId="base64value"
          Glide.with(getApplicationContext())
            .load(photoId)
            .apply(RequestOptions.circleCropTransform())  //If you get circule
            .into(employeeImage); //xml file name
Masum
  • 1,037
  • 15
  • 32