在Android中为NanoHttpD引用资产中的文件

时间:2014-03-03 09:43:36

标签: android android-assets nanohttpd

我正在尝试使用NanoHTTP在Android的帮助下提供HTML文件。 目前我可以显示页面。我也是andriod的新手。即使我在AssetManager的帮助下获取了文件,问题仍然没有清晰的图像如何引用该html文件的图像和CSS。下面我提供了整个代码。有人可以帮我这个。

注意: 其他权限设置很好,就像InternetPermission一样。只是不知道如何为html引用其他支持的文件(css,图像,javascript文件)。

package com.web;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import java.io.*;
import java.util.*;


public class MainActivity extends Activity
{
    private WebServer server;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        AssetManager assetManager = getAssets();

        String imagePath = "fileapp/image";
        String cssPath = "fileapp/css";

        String files[] = null;

        try {

            files = assetManager.list(imagePath);

            Log.i("Image", files[0]);
            Log.w("Image", files[0]);


        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e("Image ",e.getMessage());
        }

        server = new WebServer();
        try {
            server.start();
        } catch(IOException ioe) {
            Log.w("Httpd", "The server could not start.");
        }
        Log.w("Httpd", "Web server initialized.");
    }


    // DON'T FORGET to stop the server
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (server != null)
            server.stop();
    }

    private class WebServer extends NanoHTTPD {

        public WebServer()
        {
            super(8080);
        }

        @Override
        public Response serve(String uri, Method method, 
                              Map<String, String> header,
                              Map<String, String> parameters,
                              Map<String, String> files) {

            StringBuilder msg = new StringBuilder("<html><head><title>" + heading + "</title>" +
                                **"<link href=\"../../../fileapp/css/style.css\" rel=\"stylesheet\">" +**
                                "</head>"
                                + "<body>");

            msg.append("**<div class=\"jumbotron-sm\"> <img src=\"../../../fileapp/image/logo.png\">**</div>");
            msg.append("<div>My Body Content go here......</div>");
            msg.append("</body>"
                + "</html>");

            return msg.toString();
        }
    }

}

如有任何进一步的细节,请告诉我。

感谢您的答案。

0 个答案:

没有答案