simple code to read a .txt file from SDCard and display its contents(data in Text file) on WebView

WebView mWebView =(WebView)findViewById(R.id.webView1);
      mWebView.getSettings().setJavaScriptEnabled(true);
      try{
            File f = new File(Environment.getExternalStorageDirectory()+"/readme.txt");           
             FileInputStream fileIS = new FileInputStream(f);
            StringBuilder builder=new StringBuilder();
            BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
            String readString = new String();
            //just reading each line and pass it on the debugger
            while((readString = buf.readLine())!= null){           
             builder.append(readString);
               Log.d("line: ", readString);
            }         
            System.out.println("StringBuilder dataa"+builder.toString());
            String data = "<html>"  + 
            "<body><h6>"+builder.toString()+"!</h6></body>"  +"</html>";           
            mWebView.loadData(data, "text/html", "UTF-8");           
         } catch (FileNotFoundException e) {
            e.printStackTrace();
         } catch (IOException e){
            e.printStackTrace();
         }

Comments

Popular posts from this blog

Custom Dialog with no title and full screen in Android

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard

Best working code to set Date limits on Date Picker Dialog,in Android