2011-12-22 187 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); 

對不起我的英語不好:)