【问题标题】:android webpages in webviewwebview中的android网页
【发布时间】:2012-09-05 20:50:09
【问题描述】:

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

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项目文件夹中,即使没有互联网连接也可以查看?

【问题讨论】:

  • 可能与this重复

标签: android webview offline


【解决方案1】:

是的!

将它们放在/assets 文件夹中并像这样访问它们:

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

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

【讨论】:

    【解决方案2】:

    将网页存储在您的资产文件夹中并使用

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

    这会将文件写入您的存储中,并且可以由其他应用程序(如 Adob​​e)共享以打开。刚刚调用了这个方法。

    【讨论】:

    • 呃,只显示一页似乎有点过头了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多