【问题标题】:adding a image from cam to a dynamically made html string将图像从 cam 添加到动态生成的 html 字符串
【发布时间】:2015-02-05 08:39:05
【问题描述】:

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

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

有什么建议吗? 谢谢

【问题讨论】:

    标签: android android-webview


    【解决方案1】:

    您显示的链接中的一个答案建议使用 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 中图像的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 2013-08-21
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多