保存 webview 内容以供离线浏览? [英] Save webview content for offline browsing?

查看:22
本文介绍了保存 webview 内容以供离线浏览?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个应用程序,允许用户保存网页包括图像.然后用户可以再次查看网页即使没有网络访问.然而,我发现没有此类相关的 API 可以这样做.有没有人有想法或经验?任何人都可以提供一些想法或源代码链接吗?

I would like to implement an apps that allow users save the webpage including images. Then the users can view the web page again even there is no network access. However, I found that there is no such related API to do so. Does anyone have idea or experience? can any one give some idea or source code link?

推荐答案

至少对于基本页面,这应该是可能的.

At least for basic pages, this should be possible.

(1) 下载 HTML 文件,而不是直接将其加载到 WebView 中.然后使用 WebView.loadData() 或 WebView.loadDataWithBaseUrl() 将页面加载到视图中.不要丢弃 html,你以后会用到它.

(1) Download the HTML file instead of loading it directly into the WebView. Then use WebView.loadData() or WebView.loadDataWithBaseUrl() to load the page into the view. Don't trash the html, you'll need it later.

(2) 设置您自己的 WebViewClient,它覆盖 [至少] onLoadResource().然后保存页面请求的每个资源.

(2) Set your own WebViewClient which overrides [at least] onLoadResource(). Then save every single resource requested by the page.

(3) 现在您已经存储了 HTML,以及文件系统上的所有必要文件.在 HTML 中搜索绝对路径,您需要使用替换函数更新它们,以便它们是相对路径(并将在文件系统上工作)或保存图像/css/等的绝对路径.(在文件系统上)

(3) Now you have the HTML stored, and all the necessary files on the filesystem. Search through the HTML for absolute paths, you'll want to update these with a replacement function so they are either relative paths (and will work on the filesystem) or absolute paths to where you saved the images/css/etc. (on the filesystem)

(4) 将 html 写入文件系统.

(4) Write the html to the filesystem.

我希望这会有所帮助.

或者,您可以将页面另存为图像,本质上是对 WebView 进行屏幕截图.这将不允许复制/粘贴或点击链接,但如果您只需要稍后再查看",那就更简单了.

Alternatively, you could probably save the page as an image, essentially taking a screenshot of the WebView. This wouldn't allow for copy/paste, or clicking of links, but it would be much simpler if all you need is "view this again later".

这篇关于保存 webview 内容以供离线浏览?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆