如何从android中的assets文件夹中读取html内容

时间:2011-12-22 11:27:23

标签: java android

try {


    File f = new File( "file:///android_asset/[2011]011TAXMANN.COM00167(PATNA)")    ;


        FileInputStream fis= new FileInputStream(f);
        System.out.println("_______YOUR HTML CONTENT CODE IS BELLOW WILL BE PRINTED IN 2 SECOND _______");
        Thread.sleep(2000);
         int ch;
        while((ch=fis.read())!=-1)
        {
        fileContent=fileContent+(char)ch;                 // here i stored the content of .Html file in  fileContent variable

        }
        System.out.print(fileContent);

        //}
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

这是我的代码。我想从asstes文件夹中读取html内容我的文件在asstes文件夹中可用但它提供异常FileNotFoundException。那么PLZ任何人告诉我如何从android中的asstes文件夹中读取html内容?

文件f =新文件(“file:/// android_asset / [2011] 011TAXMANN.COM00167(PATNA)”); 当我调试f give = file:/ android_asset / [2011] 011TAXMANN.COM00167(PATNA)

请告诉我如何获取corrct目录以及我在哪里做错了它让我来了文件:/// android_asset / [2011] 011TAXMANN.COM00167(PATNA)

3 个答案:

答案 0 :(得分:1)

这是从WebView中的资源加载HTML文件的方法

 webview.loadUrl("file:///android_asset/Untitled-1.html");

Untitled-1.html ---应首先保存为.html扩展名的文件名

<强>被修改

试试此链接

http://developer.android.com/reference/android/content/res/AssetManager.html

这个doc有方法 public final String [] list(String path)

答案 1 :(得分:1)

你可以通过以下代码获取InputStream: getResources().getAssets().open("you_file_name_goes_here");

答案 2 :(得分:1)

您不想使用

webview.loadUrl('file:///android_asset/htmlFile.html');

正确?

尝试这个我在博客中找到它:

    static String getHTMLDataBuffer(String url,Context context) {
    InputStream htmlStream;
    try {
    if (Utils.isReferExternalMemory() && url.contains("sdcard")) {
    String tempPath = url.substring(7, url.length());//remove file:// from the url
    File file = new File(tempPath);
    htmlStream = new FileInputStream(file);
    }else{
    String tempPath = url.replace("file:///android_asset/", "");
    htmlStream = context.getAssets().open(tempPath);
    }
    Reader is = null;
    try {
    is = new BufferedReader(new InputStreamReader(htmlStream, "UTF8"));
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    // read string from reader
    final char[] buffer = new char[1024];
    StringBuilder out = new StringBuilder();
    int read;
    do {
     read = is.read(buffer, 0, buffer.length);
     if (read>0) {
       out.append(buffer, 0, read);
     }
    } while (read>=0);

    return out.toString();  
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return null;
    }

用法:

      String data = getHTMLDataBuffer("file:///android_asset/yourHtmlFile",this);
      webview.loadDataWithBaseURL("http://example.com", data, "text/html", "utf-8", null);

抱歉我的英文不好:)