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

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)

karthik
  • 17,453
  • 70
  • 78
  • 122
Joan
  • 61
  • 2
  • 10

3 Answers3

1

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

Ahmed Sabry
  • 434
  • 1
  • 8
  • 17
1

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)

Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
1

You cat get InputStream by this code: getResources().getAssets().open("you_file_name_goes_here");

muffinmad
  • 641
  • 3
  • 9