4

My app loads a small HTML document that contains one image in a webview. How can I fetch this image and use it as a Bitmap object in my app?

I'm already using a JavaScriptInterface together with my webview for getting some other information, like passing booleans. Is it possible to pass an image aswell via the JavaScriptIterface? Is it a good idéa or is there a better way?

prograde
  • 2,620
  • 2
  • 23
  • 32
  • The problem is that i don't want to load the image one more time for specific reasons. Otherwise it would have been the obvious solution. I have loaded it once, and I want to re-use the image. – prograde Mar 21 '12 at 00:51

1 Answers1

2

Take a look at this question: Get image data in JavaScript?

You might be able to draw the image on a (I presume hidden) Canvas, then Base64-encode it with toDataURL and pass that as a string through the JS interface then decode it on the Java side. I imagine it'll be slow, but it's worth a try.

Community
  • 1
  • 1
kabuko
  • 36,028
  • 10
  • 80
  • 93
  • Thanks! I tried this, but the JavaScript seems to crash when executing ctx.drawImage(img, 0, 0); I also need some advice on how to convert the string to a Bitmap object in the app. – prograde Mar 21 '12 at 16:33
  • Also, as I understand it, the toDataURL method works for png, might work for jpg, but not for gif. Is that correct? – prograde Mar 21 '12 at 16:39
  • Unfortunately I can't say I've tried this so I can't help you too much with the details. It was just something that conceptually I thought might be helpful to you. – kabuko Mar 21 '12 at 17:14
  • 1
    I solved the convert to Bitmap problem (If someone is interested: imageView.setImageBitmap(BitmapFactory.decodeByteArray(Base64.decodeBase64(imgString.getBytes(), 0, imageAsBytes.length)));). It works if I create an image in the Javascript, but it still crashes when I try the drawImage method. – prograde Mar 21 '12 at 18:34