2011-12-22 15 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です。どのようにAndroidのasstesフォルダからhtmlコンテンツを読むか教えてください。androidのassetsフォルダからhtmlコンテンツを読む方法

ファイルf =新しいファイル( "file:/// android_asset/[2011] 011TAXMANN.COM00167(PATNA)");/android_asset/[2011] 011TAXMANN.COM00167(パトナ)

はplzはどのようにcorrctディレクトリを取得し、どこイムやって、それは私が来てshud間違ったファイルを教えてください::/// android_asset/[ 私はデバッグfは=ファイルを提供します2011] 011TAXMANN.COM00167(パトナ)

答えて

1

これはWebViewの中で.html拡張子として最初に保存する必要があります

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

無題-1.html ---ファイル名を資産からHTMLファイルをロードする方法です

編集

このリンクを試してみてください

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

方法はこのdocからあり 公共最終String []型のリスト(文字列のパス)

+0

ファイルf =新しいファイルの

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

申し訳ありません「/ファイル:

これを試してみてください、私はブログでそれを見つけました/ //android_asset/[2011]011TAXMANN.COM00167(PATNA) "); – Joan

+0

私はこれを行うことができません私はそのファイルの内容を表示しないようにしたい.. – Joan

+0

あなたはタグ値でタグを読み取ることを意味しますか – Sameer

1

あなたの猫このコードでInputStreamを取得するには: getResources().getAssets().open("you_file_name_goes_here");

+0

Exception File not Exception ... – Joan

+0

あなたはopen – muffinmad

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

使用方法:(私の悪い英語:)

関連する問題