Android Web服务器应用程序使用保存的图像生成HTML

时间:2018-04-24 00:42:28

标签: android html webserver

我是Android新手开发者。这是我的第一篇文章。我已经寻找了一个可能的答案,并没有找到任何有用的东西。基本上,我正在将图像文件成功保存到/data/user/0/com.example.testing/files。我需要创建一个包含这些图像文件的HTML文档。这一切都发生在同一个应用程序中。实际上,所有代码目前都在MainActivity类中。我对与其他应用程序共享图像不感兴趣,只想拥有一个非常简单的Web服务器来显示图像。有关如何存储图像以便使用我的Web服务器输出的任何想法?感谢您提供的任何帮助。

代码段

保存代码:

try {
    FileOutputStream fos = openFileOutput(localFileName, 0); // was Context.MODE_PRIVATE);
    bitMap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.close();
} catch (Exception e) {
    Log.e("saveToInternalStorage() error", e.getMessage());
    return false;
}

制作HTML:

    sendMessage += "<img src=\"" + localFileName + "\"></br></br>";

HTML回复代码:

try {
    httpServerSocket = new ServerSocket(HttpServerPORT);

    while(true){
        socket = httpServerSocket.accept();

        HttpResponseThread httpResponseThread =
                new HttpResponseThread(
                        socket,
                        sendMessage);
        httpResponseThread.start();
    }
} catch (IOException e) {
    e.printStackTrace();
}

1 个答案:

答案 0 :(得分:0)

据我所知,根据权限,我不能拥有可以远程访问的“世界可读”文件,例如使用Web浏览器。最终的解决方案是生成base64编码的文件,并执行以下操作:

<img src=data:image/jpg;base64,[lots of base64 decoded bytes]>

它有点混乱,需要花费大量时间来处理。特别是如果它们是视频文件:

<video><source type="video/mp4" src="data:video/mp4;base64,[lots of base64 bytes]"></video>

我想知道是否有更有效的方法来做到这一点。