【问题标题】:Save webpage for offline use and invoke the same android保存网页以供离线使用并调用相同的android
【发布时间】:2013-07-03 06:36:34
【问题描述】:

我有一个 android 应用程序要求,我需要打开保存的网页,怎么做?首先,我们如何保存一个依赖于 android 的网页,然后在您的应用程序中打开它?任何输入都会有很大帮助!

【问题讨论】:

    标签: android android-webview


    【解决方案1】:

    这就是我的实现方式:

    • 将原始网页保存到文件中
    • 解析保存的文件并获取所有图像 URL。将图像保存到同一目录中。
    • 转换图片的URL(将所有链接绑定到本地副本)

    下面是演示这个想法的简单代码:

    String download(String url) throws Exception {
        String filename = "local.html";
        save(url, filename);
    
        List<String> imageLinks = getImageURLs(filename);
        for (String imageLink : imageLinks) {
            String imageFileName = getImageName(imageLink);
            save(imageLink, imageFileName);
        }
    
        convertImageURLs(filename);
        return filename;
    }
    
    void save(String url, String saveTo) throws Exception {
        HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        save(is, saveTo);
    }
    
    void save(InputStream is, String saveTo) {
        // save actual content
    }
    
    List<String> getImageURLs(String localHtmlFile) {
        // parse localHtmlFile and get all URLs for the images
        return Collections.EMPTY_LIST;
    }
    
    String getImageName(String imageLink) {
        // get image name, from url
        return null;
    }
    
    void convertImageURLs(String localHtmlFile) {
        // convert all URLs of the images, something like:
        // <img src="original_url"> -> <img src="local_url">
    }
    

    【讨论】:

    • 您好,谢谢您的回复,这似乎很复杂,有没有像我们在桌面上那样简单的方法?
    • 看看this post,看来这就是你要找的。​​span>
    【解决方案2】:

    首先,让我们从 webview

    中保存 webarchive
    private void initUI{
         webView = (WebView) findViewById(R.id.webView);
         AndroidWebClient client = new AndroidWebClient();
         webView.setWebViewClient(client);
         WebSettings webSettings = webView.getSettings();
         webSettings.setJavaScriptEnabled(true);
    }
    private class AndroidWebClient extends WebViewClient {
            @Override
            public void onPageStarted(WebView view, String url,
                                      android.graphics.Bitmap favicon) {                
            }
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                view.saveWebArchive(Environment.getExternalStorageDirectory()
                        + File.separator +"myArchive"+".mht");
                // our webarchive wull be available now at the above provided location with name "myArchive"+".mht"
    
            }
            public void onLoadResource(WebView view, String url) {
    
            }
          }
    

    保存 webarchive 的方式在所有 API 中都是相同的,但加载它的方式不同

    对于低于 Kitkat 的 API

    private void loadArchive(){
         String rawData = null;
            try {
                rawData =   getStringFromFile(Environment.getExternalStorageDirectory()
                        + File.separator+"myArchive"+".mht");
            } catch (Exception e) {
                e.printStackTrace();
            }
    webView.loadDataWithBaseURL(null, rawData, "application/x-webarchive-xml", "UTF-8", null);
    }
    
      public String getStringFromFile (String filePath) throws Exception {
            File fl = new File(filePath);
            FileInputStream fin = new FileInputStream(fl);
            String ret = convertStreamToString(fin);
            //Make sure you close all streams.
            fin.close();
            return ret;
        }
    
        public  String convertStreamToString(InputStream is) throws Exception {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            reader.close();
            return sb.toString();
        }
    

    适用于 Kitkat 及更高版本

    private void loadArchive(){
       webView.loadUrl("file:///"+Environment.getExternalStorageDirectory()
                    + File.separator+"myArchive"+".mht");
    }
    

    【讨论】:

    • 嗨.. 我正在按照相同的步骤以 .mht 格式保存和加载网页。但是当我在 webView 上加载 .mht 文件时,webView 显示为空白。这可能是什么问题?
    猜你喜欢
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2015-05-04
    • 1970-01-01
    相关资源
    最近更新 更多