2012-09-05 50 views
0

我想在android项目文件夹中存储网页,以便用户不需要互联网连接来查看网页。我正在使用android webview。我能够使用HTTP协议查看网页。我的代码如下:在webview中的android网页

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

但我想看到网页离线。有没有什么办法,网页可以存储为Android项目文件夹中的资源,并查看即使没有互联网连接?

+0

可能会重复[this](http://stackoverflow.com/questions/4563291/save-webpages-for-offline-browsing) – Shrikant

回答

1

是!

,把它们放进/assets文件夹,并访问他们像这样:

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

这个问题已经有了答案:Webview load html from assets directory

0

店在您的资产文件夹中的网页,并使用

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

这会将文件写入存储并可以由另一个应用程序(如Adobe)共享以打开。只是称这种方法。

+1

呃,这似乎是矫枉过正只是显示一个页面。 –

相关问题