如何从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文件夹中可用但是它给出了exceptionFileNotFoundException
。 那么任何人都告诉我如何从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)
这是从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)的方法
你可以通过以下代码获取InputStream: getResources().getAssets().open("you_file_name_goes_here");
你不想用
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);
对不起,我的英语不好 :)