0

I want to add a image that i take from phones camera and add it to a html that is build dynamically from user input. so i cant get the html file from assets.

all the question/answers i found are for local images and html from assets. Android - local image in webview Android Development: Using Image From Assets In A WebView's HTML

any suggestions? Thanks

Community
  • 1
  • 1
ilan
  • 4,402
  • 6
  • 40
  • 76

1 Answers1

0

One of the answers in the links you showed, suggests to use Base64 embedded image:

(https://stackoverflow.com/a/9000691/1271435)

  <img src="data:image/jpg;base64,xxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>

  xxxxx = base64 encoded string of images bytes

This sounds like a good idea, and one way to do it is something like this:

After taking the image with camera you should get back an imageUri of the photo.
then:
1.Get the input stream of that image:
InputStream stream = getContentResolver().openInputStream(imageUri);

2.Fill in the stream into a ByteArrayOutputStream:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
fillStream(stream, baos); // dummy method, provide implementation

3.Convert the byte array to Base64:
String base64Image = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));

Then you can use the base64 string as the data for your image in HTML.

Community
  • 1
  • 1
Andy Res
  • 15,963
  • 5
  • 60
  • 96