1

我正在做一个项目,其中我的 android 用作 Web 服务器;输入带有端口号的 IP 地址,然后打开一个 Web 界面,用户可以将文件上传到手机。我想在 Web 界面上显示一些图片,以便我们的界面看起来不错。

如何在图像 src"" 文件中引用可绘制或原始或资产文件夹中的图像

这是我的代码:

     private String getZipLink(long folderId) {
     return "<a href=\"/zip/" + folderId + "/folder.zip\"><img src=\"file:///android_res/raw/img\" />" +
     "Zip of Entire Folder</a>";

如何在那里添加图像?}

4

3 回答 3

1

也许您可以在 HTML 中嵌入图像数据(而不是尝试提供指向图像数据的链接):请参阅Inline Images with Data URLs

于 2012-12-09T17:05:36.090 回答
0

如果您可以控制 Web 服务器中的请求处理,则可以使用图像 URL,例如

<img src="images/myimage.png" ... />

images/myimage.png并通过返回具有正确 MIME 类型的字节流来处理对资源的请求image/png。这与 Java EE Servlet 中使用的代码非常相似,因此您可以搜索示例代码并使其适应您的环境。

于 2012-12-09T17:11:04.363 回答
0

你可以做这样的事情

   /**
     * This method starts the web server listening to the port 8080
     */
    protected void start() {

        new Thread(new Runnable() {

            @Override
            public void run() {
                Log.d(TAG, "Secure Web Server is starting up on port 8080");
                try {
                    // Create the secure server socket
                    sss = (SSLServerSocket) sssf.createServerSocket(8080);
                } catch (Exception e) {
                    System.out.println("Error: " + e);
                    return;
                }

                Log.d(TAG, "Waiting for connection");
                while (isRunning) {
                    try {
                        // Wait for an SSL connection
                        Socket socket = sss.accept();

                        Runtime rt = Runtime.getRuntime();
                        Process pr = rt.exec("lsof -i:"+socket.getPort());
                        System.out.println("Process:::"+pr.getInputStream());

                        InputStream in1 = pr.getInputStream();
                        InputStreamReader is = new InputStreamReader(in1);
                        StringBuilder sb=new StringBuilder();
                        BufferedReader br = new BufferedReader(is);
                        String read = br.readLine();

                        while(read != null) {
                            //System.out.println(read);
                            sb.append(read);
                            read =br.readLine();

                        }

                        System.out.println("Process read:::"+sb.toString());

                        // Got a connection
                        Log.d(TAG, "Connected, sending data.");

                        BufferedReader in = new BufferedReader(
                                new InputStreamReader(socket.getInputStream()));
                        PrintWriter out = new PrintWriter(socket
                                .getOutputStream());

                        // Read the data until a blank line is reached which
                        // signifies the end of the client HTTP headers
                        String str = ".";
                        while (!str.equals(""))
                            str = in.readLine();

                        // Send a HTTP response
                        out.println("HTTP/1.0 200 OK");
                        out.println("Content-Type: text/html");
                        out.println("Server: Android KeyChainiDemo SSL Server");
                        // this blank line signals the end of the headers
                        out.println("");
                        // Send the HTML page
                        out.println("<H1>Welcome to Android!</H1>");
                        // Add an embedded Android image
                        out.println("<img src='data:image/png;base64," + base64Image + "'/>");
                        out.flush();
                        socket.close();
                    } catch (Exception e) {
                        Log.d(TAG, "Error: " + e);
                    }
                }
            }
        }).start();

    }





base64Image = createBase64Image(ctx);


 /**
     * This method reads a binary image from the assets folder and returns the
     * base64 encoded image string.
     *
     * @param ctx The service this web server is running in.
     * @return String The base64 encoded image string or "" if there is an
     *         exception
     */
    private String createBase64Image(Context ctx) {
        BufferedInputStream bis;
        try {
            bis = new BufferedInputStream(ctx.getAssets().open(**image file stored in assets folder **));
            byte[] embeddedImage = new byte[bis.available()];
            bis.read(embeddedImage);
            return Base64.encodeToString(embeddedImage, Base64.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

所以把你的图片放在android的assets文件夹中

有关更多说明,请参阅 Android4.0 的 Android 示例中的 Android APIDemos 中的 KeyChainDemo

于 2012-12-09T17:11:36.567 回答