Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

java - Save webview content for offline browsing?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

At least for basic pages, this should be possible.

(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) Set your own WebViewClient which overrides [at least] onLoadResource(). Then save every single resource requested by the page.

(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) Write the html to the filesystem.

I hope this helps.

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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...