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

发布于 2024-12-22 06:11:47 字数 1283 浏览 0 评论 0原文

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。那么请有人告诉我如何从 android 中的 asstes 文件夹中读取 html 内容?

文件 f = new File(“file:///android_asset/[2011]011TAXMANN.COM00167(PATNA)”) ; 当我调试 f 给出= file:/android_asset/[2011]011TAXMANN.COM00167(PATNA) 时,

请告诉我如何获取正确的目录以及我做错的地方,它让我感到震惊 file:///android_asset/[2011]011TAXMANN.COM00167 (帕特纳)

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

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?

File f = new File( "file:///android_asset/[2011]011TAXMANN.COM00167(PATNA)") ;
when i debug f gives= file:/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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终弃我 2024-12-29 06:11:47

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

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

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

已编辑

尝试此链接

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

这个文档有方法
公共最终字符串[]列表(字符串路径)

This is the way to load HTML file from assets in WebView

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

Untitled-1.html---File name that should be save first as .html extension

Edited

try this link

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

there is method from this doc
public final String[] list (String path)

顾冷 2024-12-29 06:11:47

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

You cat get InputStream by this code:

getResources().getAssets().open("you_file_name_goes_here");

层林尽染 2024-12-29 06:11:47

你不想用对

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

抱歉我的英语不好:)

you don't want to use

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

right?

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

usage:

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

Sorry for my bad english :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文