Download and display html files - android

I have to download contents of an HTML file (which uses image) via REST API. Then store them for offline use. When needed I want to display the HTML page with a WebView (of course with image). The REST result should look something like:
{
"id": 45,
"html": "<!DOCTYPE html><html><body><h1>My Heading</h1><p>My paragraph.</p><img src='abc.com/images/54289847.jpg' alt='any' width='104' height='142'></body></html>",
"imagePath": "abc.com/images/54289847.jpg",
"moreData": "something more"
}
So, what will be the most efficient way to do this?
I'm planning encrypted contents of HTML will go to local storage in any file. Same thing for the images. And then decrypt, and load to WebView. But will it work for images?
I don't need any code, just suggest me a way.

in this example string is loaded to webview
String summary = "You scored 192 points.";
webview.loadData(summary, "text/html", null);
what if you get string from input stream and pass it to web view IOUtils.toString(context.getResources().openRawResource())

Im not an android developer, but can't you get the "image data-url" and use as the image itself?
For example your avatar's data-url is:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAADYUlEQVRYhe2W7UtTYRjGj2VqZSFC4AtSS+d8Wa5pKaWbnql7dVMsxEjDisjMVAoKkvxgUEFZhJVT6MWioBeCvkX0j/SxP0D6srOzuZ1zdZ9ztnl0x9cZEvjh5kx39jy/+7qv+34ehhnwYUtjG+D/AbiqipX+t+kA0uLDPhT2enCxw4VJuwNTLXb4Ke46HXCddiPzQtvCu5sKMOJDTbcH362tEA02oJgFypdECYs/lc2453Ag+3Lb+iGW/WLIi3HKEBUW2sQC7jCLQIV2CDGYOWMznKSIBJ4awDUvPrJ2oJBFuLMckTs54L1GBHQOBKoalwWRFdHb0N/uTAGA6MdclLmOBVdmQfRJBvCVgTiVicjoAYROlSNQbltZDYKwdLnXVo6lhjOf8QClNnDSgpVN4FkThIls4A0DfGIQ7tMhkOdBoMy2OFRQkhK/Tc1IG/CuE4Dc/oMMJy0gLyY9i5wItZohTmQCMwyio7ngm46BbzCDt1I0VNOzGsH6Ohk4AUGGvd7mXF0Fdfa6HsreEMuegjNbMD9cBFHK3p8GTDMyBGbTFEVmGeX5gcAIkKs9qUDHVPhV1wJmcBUV1ACDPie5fiF7ru4EhGc7gXeMsrlWzMRK01NCJl3wgpSEVEo9JbWiCuo/pmm4JOSPL6RvQWQoX9lIA0Cc3I35/iJwpc2J7NVl6O10rRHgig8/La1JAIGDDgXgrcbmM+ngJdmLXOCO1Ce3JcWljlQBaADN3yhILgMpIvozECSFeHcVIg+zwBmsKQAsUwKpvXjrUYgv0xcD+CneM7I6kZE8iGRITuoCdTuuqwRLTagC4IxNEJ9nLfjgxQ4I4/sRpQkZvZ0L8RUjl4i3mZSZsCETarRhwohkMOHRPgVgilrwdRqEx9kIn9XTHKhFdCwH+EydcJ6G1CHbBttQaxAljMgicitf3kR4sAfC010IdRkQtB6nDR0ItRuAL1SOwQL53Y0NIq1RrJoJvMuE8Lli2XBBtkbZSALV2RDyGYFvBHAzn4BSGcXqw6iYTSqF1BGyydSHEX2WgKL39yLcXQpB35jCYRSP+HGsBaEVZFLebqb3rfJvUjuO4xG/kFA2kqQrgchHsMmCORO7SRcSVTlqut1rv5L1beaVTGXMrbuULgXZkmv5v45tgK0G+At38NtPjlXm2AAAAABJRU5ErkJggg==
If you paste this in your computer's browser(as url) and/or in <img> tag, you'll see your avatar.

Related

All webview content printed in one page

Case: User should be able to view and print pdf
My solution: I am opening PDF inside Webview with the help of docs.google.com/gview. Below is my code
Set up Webview
string url = "http://www.africau.edu/images/default/sample.pdf";
string gview = $"https://docs.google.com/gview?embedded=true&url={url}";
mWebView.LoadUrl(gview);
Print PDF
var printMgr = (PrintManager)GetSystemService(PrintService);
printMgr.Print("print", mWebView.CreatePrintDocumentAdapter("print"), null);
Below is the screenshot. As you can see PDF loads just fine
Problem
When I want to print PDF, all the PDF pages are printed in one paper which you can see below
I would appreciate any suggestion, including different library for displaying/printing pdf or suggestion in Java or Kotlin, I can convert them to C#.
I would not print the web page but print the PDF directly as when printing the web page it just sees it as a longer web page and knows nothing about the content.
Use a custom print adapter instead, but instead of drawing a PDF to print you can just use the existing PDF you already have.
See for details https://developer.android.com/training/printing/custom-docs.html

Loading of base64 images in WebView

I've got a question related to the showing HTML data inside WebView.
I have quite big HTML string with some images with src in base64 format. I'm reading this data from file. File reading is quite fast and I have no issues with this.
When it's only one image inside HTML - it works fine. But when at least a couple - it's lagging a lot. (It's loading about 30-35 seconds)
I tried to speed up it but no result.
This is the way I load HTML string.
WebView.loadDataWithBaseURL("file:///", StringEscapeUtils.unescapeHtml4(hmtl), "text/html", "UTF-8", null);
I use this function with base URL "file:///" because I'm going through the links(local) inside this file. So I guess it's obligatory to use this kind of function.
Can anyone help me with a solution?

How to clear html page before showing into a webview in Android?

I have the URL of a webpage to be displayed into a webview in my Android app. Before showing this page i want to clear the html code of this page from some tag (such as the header, footer, ecc..) in order to show only few information. How can i do it? I tried to solve the issue working with JSoup but i can't understand how to create and pass the "new page" to the webview. Anybody can help me?
EDIT
I cleaned the html code useless through jsoup libraries. Then, always by mean of these, i get head and body content and finally i showing the "cleared" web page through these lines:
headURL = doc.select("head").outerHtml();
bodyURL = doc.select("body").outerHtml();
webview.loadData( "<html>"+headURL+bodyURL+"</html>" , "text/html", "charset=UTF-8");
webview.setWebViewClient(new DisPlayWebPageActivityClient());
The view shows the new page but do not load css files specified in the head(that has not been touched). Who can say me why?
You can fetch the WebPage you want to display as a string, parse and remove whatever you don't want and then load this string as data in your webview.
Something like:
String webContent = fetchPage(url);
String cleanedWebContent = cleanUp(webContent);
webView.loadData(cleanedWebContent, "text/html", "UTF-8");
Of course, you will need to implement fetchPage and cleanUp as they are not Android methods

How can I embed the new GIFV format on my android app?

I would like to know if one of your could embed GIFV format into your app. I am trying to embed it using a webview but without success.
Actually GIFV format is closer to a video format than a gif.
Whatever suggestion would be nice. Thanks in advance.
After work around of this issue, I couldn't find a solution which could display GIFV format like a video.
But, my solution to this issue has been to use a webview using the same way that i was using to make able to load "gif" files and if you remove directly the "v" from the name of the file, you can show it as a .gif file.
Resuming, remove the "v" from the .gifv file name and display it as a .gif.
The way that i am injecting code to make able to display gifs into the webview is:
public void getContentWebView(String url, WebView webView)
{
String html = "<!DOCTYPE html><html><body><img src=\""+ url +"\" width=\"100%\" height=\"100%\"></body></html>";
webView.loadData(html, "text/html", "utf-8");
}

Android webview image fullscreen on click

I am working on a developing an Android application that displays news articles from a database through JSON. The article is in HTML format because the database is used for both web and the app. The code I have (below) works great. The format is the same on both web and phone when displayed in a webview, but I would like the images to be clickable, so they can can be loaded in a separate activity, and the user can zoom and such.
I guess I am just not using the proper wording when looking for an answer, because I cannot find anything that relates to this. I am assuming I would have to find the tags on click and capture the url somehow, and then pass it to another activity. I am not sure if this is the best way to do this or not. Any insight on this would be greatly appreciated.
web = (WebView) findViewById(R.id.WebView01);
final String mimeType = "text/html";
final String encoding = "UTF-8";
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.setScrollbarFadingEnabled(false);
web.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
web.loadDataWithBaseURL("", product.getString(TAG_CONTENT), mimeType, encoding, "");
This code runs within a Async task that queries the database for info.
Since it is HTML, you can use the onclick attribute of the img tag
<img src="myimage.png" onclick="javascript:window.location=this.src;" />
This will open the image up as the current window.
Or you can do something similar to this answer and send the URL to another activity.

Categories

Resources