0

i want to store webpages inside the android project folder so that a user does nor needs a internet connection to view the webpages. i am using android webview. i am able to see the webpages with the HTTP protocol . My code is as below:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        WebView webview = (WebView) findViewById(R.id.webView1);
       // webview.loadUrl("http://www.mysite.com/index.html");

        webview.getSettings().setJavaScriptEnabled(true);
    }

but i want to see the webpages offline. is there any way that webpages can be stored as resource in android project folder and view even without internet connection?

imran
  • 626
  • 2
  • 13
  • 24

2 Answers2

1

yes !

Put them in the /assets folder and access them like this :

webview.loadUrl("file:///android_asset/my_html_page.html"); 

This questions already have been answered : Webview load html from assets directory

Community
  • 1
  • 1
Mathieu de Brito
  • 2,536
  • 2
  • 26
  • 30
0

store the webpage in your Asset folder and use

         public  File getfile(String filename) throws IOException {
    // TODO Auto-generated method stub
   String externalStorage_path          =Environment.getExternalStorageDirectory().toString();
   String state = Environment.getExternalStorageState();
   if (Environment.MEDIA_MOUNTED.equals(state)){
        File dir = new File(externalStorage_path + "/yourfilename");
        dir.mkdir();
        File mfile = new File(dir,filename);
        if( mfile.exists()==true)  return  mfile;
        else{
              try{
                  InputStream myInput =  mcontext.getAssets().open(filename);
                  String path =externalStorage_path+"/yourfilename";
                  OutputStream  myOutput = new FileOutputStream (path);
                  byte[] buffer = new byte[1024];
                  int length;
                  try {
                      while((length = myInput.read(buffer))>0)
                      myOutput.write(buffer,0,length);
                  }catch(FileNotFoundException e){Log.d("error",""+ e.toString());
                  }finally{
                      myOutput.flush();
                      myOutput.close();
                      myInput.close();
                  }
              }catch(IOException e){ }
              File dir1 = new File(externalStorage_path + "/yourfilename");
              dir1.mkdir();
              File mfile1 = new File(dir,filename);
              return mfile1;
        }
   }else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
       showToast("External storage has readonly access");
   } else if (Environment.MEDIA_REMOVED.equals(state)) {
       showToast("External storage not present");
   } else if (Environment.MEDIA_UNMOUNTABLE.equals(state)){
       showToast("External storage cannot be mounted. Sdcard problem");
   }

this will write the file in your storage and can be share by another Application like Adobe to open. jUST called this method.

Rajendra
  • 1,700
  • 14
  • 17