加载托管在资产文件夹中的图像文件,在应用程序中使用 Web 服务器公开

loading an image file hosted in assets folder exposed with web server in the app

在我的应用程序中,我创建了托管网络应用程序的网络服务器。 Web 应用程序的所有文件都放在 assets 文件夹中。

现在,我通过 运行 启动我的应用程序来启动网络服务器,然后从 crome 浏览器,我尝试通过调用 index.html 文件来 运行 我的网络应用程序。页面的 html、css 部分已正确加载,但图像未加载到页面中:

这是我的 HttpRequestHandlerCode:

public class HomePageHandler implements HttpRequestHandler {
    private Context context = null;

    private static final Map<String, String> mimeTypes = new HashMap<String, String>() {

        {
            put("css", "text/css");
            put("htm", "text/html");
            put("html", "text/html");
            put("xhtml", "text/xhtml");
            put("xml", "text/xml");
            put("java", "text/x-java-source, text/java");
            put("md", "text/plain");
            put("txt", "text/plain");
            put("asc", "text/plain");
            put("gif", "image/gif");
            put("jpg", "image/jpeg");
            put("jpeg", "image/jpeg");
            put("png", "image/png");
            put("svg", "image/svg+xml");
            put("mp3", "audio/mpeg");
            put("m3u", "audio/mpeg-url");
            put("mp4", "video/mp4");
            put("ogv", "video/ogg");
            put("flv", "video/x-flv");
            put("mov", "video/quicktime");
            put("swf", "application/x-shockwave-flash");
            put("js", "application/javascript");
            put("pdf", "application/pdf");
            put("doc", "application/msword");
            put("ogg", "application/x-ogg");
            put("zip", "application/octet-stream");
            put("exe", "application/octet-stream");
            put("class", "application/octet-stream");
            put("m3u8", "application/vnd.apple.mpegurl");
            put("ts", " video/mp2t");
        }
    };

    public HomePageHandler(Context context){
        this.context = context;
    }

    @Override
    public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
        //String contentType = "text/html";
        //Log.i("Sushill", "..request : " + request.getRequestLine().getUri().toString());
        final String requestUri = request.getRequestLine().getUri().toString();
        final String contentType = contentType(requestUri);
                        String resp = Utility.openHTMLStringFromAssets(context, "html" + requestUri);
                        writer.write(resp);
                        writer.flush();
//                    }
                }
            });
            ((EntityTemplate) entity).setContentType(contentType);
            response.setEntity(entity);
        }
    }

    /**
     * Get content type
     *
     * @param fileName
     *            The file
     * @return Content type
     */
    private String contentType(String fileName) {

        String ext = "";
        int idx = fileName.lastIndexOf(".");
        if (idx >= 0) {
            ext = fileName.substring(idx + 1);
        }
        if (mimeTypes.containsKey(ext)) {
            //Log.i("Sushill", "...ext : " + ext);
            return mimeTypes.get(ext);
        }
        else
            return "application/octet-stream";
    }

为了处理图像,我尝试了这个但是没有用:

if(contentType.contains("image")) {
                    InputStream is = Utility.openImageFromAssets(context, "html" + requestUri);
                    char[] buffer = new char[1024];
                    try {
                        Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                        int n;
                        while ((n = reader.read(buffer)) != -1) {
                            writer.write(buffer, 0, n);
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

谁能帮我弄清楚如何在我的浏览器中加载图像。

感谢您的帮助

取消 BufferedReader(new InputStreamReader' 所以你也取消了 UTF-8。只使用 InputStream 'is'。取消 writer。你没有展示 'writer' 是什么,但是做远离它。使用http连接的OutputStream。保留缓冲区和您在缓冲区中读取并从缓冲区写入的循环。