将cam中的图像添加到动态制作的html字符串中

时间:2015-02-05 08:39:06

标签: android android-webview

我想添加一个从手机相机中拍摄的图像 并将其添加到从用户输入动态构建的html中。 所以我无法从资产中获取html文件。

我找到的所有问题/答案都是针对本地图像和来自资产的HTML。 Android - local image in webview Android Development: Using Image From Assets In A WebView's HTML

有什么建议吗? 感谢

1 个答案:

答案 0 :(得分:0)

您展示的链接中的一个答案建议使用Base64嵌入式图像:

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

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

  xxxxx = base64 encoded string of images bytes

这听起来不错,一种方法就是这样:

使用相机拍摄图像后,您应该返回照片的imageUri 然后:
1.获取该图像的输入流:
InputStream stream = getContentResolver().openInputStream(imageUri);

2.将流填入ByteArrayOutputStream

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

3.将字节数组转换为Base64:
String base64Image = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));

然后,您可以使用base64字符串作为HTML中图像的数据。