2011-12-22 146 views
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。所以PLZ任何一个告诉我如何阅读Android中的asstes文件夹的HTML内容?如何从android资源文件夹中读取html内容

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

PLZ告诉我怎么去CORRCT目录并且是即时做错了它shud我走来文件:/// android_asset/[ 2011] 011TAXMANN.COM00167(巴特那)

回答

1

这是加载从资产HTML文件中的WebView

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

无题 - 1.HTML ---文件名应先保存为.html扩展的方式

编辑

尝试此链接

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

有方法从此doc 公众最终的String [] list(字符串路径)

+0

文件f =新的文件(“/文件:/ //android_asset/[2011]011TAXMANN.COM00167(PATNA)“); – Joan 2011-12-22 11:37:00

+0

无法做到这一点,我想要读取该文件的内容不显示.. – Joan 2011-12-22 11:40:56

+0

你是什么意思,你想阅读标记值 – Sameer 2011-12-22 11:43:59

1

你的猫得到的InputStream这段代码: getResources().getAssets().open("you_file_name_goes_here");

+0

显示异常文件未发现异常... – Joan 2011-12-22 12:08:35

+0

你传递什么参数来打开()? – muffinmad 2011-12-22 13:23:30

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

对不起我的英语不好:)

相关问题