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?

1
  • 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. Commented Mar 21, 2012 at 0:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
Also, as I understand it, the toDataURL method works for png, might work for jpg, but not for gif. Is that correct?
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.