如何从android中的assets文件夹中读取html内容 [英] how to read html content from assets folder in android

查看:168
本文介绍了如何从android中的assets文件夹中读取html内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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内容?

This is my code. I want to read html content from asstes folder my file is available in asstes folder But it gives exception FileNotFoundException. So plz any one tell me how to read html content from asstes folder in android?

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

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

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

plz tell me how to get corrct directory and where i m doing wrong it shud me coming file:///android_asset/[2011]011TAXMANN.COM00167(PATNA)

推荐答案

你不要我想使用

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

对吗?

试试我在博客中找到它:

try this i found it in a blog:

    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);

抱歉我的英文不好:)

这篇关于如何从android中的assets文件夹中读取html内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆