Android WebView shows html instead of the pdf or document - android

I have a webview with javascript enabled and I'm attempting to display pdf, docs, etc... using google docs view url. The problem is that the webview is rendering html instead of the doc. I should mention that this also occurs with a url to a pdf or doc from my dropbox account.
string pdf = "https://docs.google.com/document/d/1SGFAxaYSA2wBvKCKL89rYG4yEZIJVIJ2lY-1G7IF6g4/edit?usp=sharing";
webViewcontent.LoadUrl("https://docs.google.com/gview?embedded=true&url=" + pdf);
This is what is showing:

I think the url you are looking for is...
string url = "https://docs.google.com/document/d/1SGFAxaYSA2wBvKCKL89rYG4yEZIJVIJ2lY-1G7IF6g4/edit?usp=sharing";
string myUrl = string.Format("https://docs.google.com/viewer?url={0}", url);
webViewcontent.LoadUrl(myUrl);
This will work without the user having a google account.
However, since your url is using https it may require authentication.

string pdf = "https://docs.google.com/document/d/1SGFAxaYSA2wBvKCKL89rYG4yEZIJVIJ2lY-1G7IF6g4/edit?usp=sharing";
webViewcontent.LoadUrl("https://docs.google.com/gview?embedded=true&url=" + pdf);
the final url of this will be https://docs.google.com/gview?embedded=true&url=**https://docs.google.com/document/d/**1SGFAxaYSA2wBvKCKL89rYG4yEZIJVIJ2lY-1G7IF6g4/edit?usp=sharing, I think you want to remove everything except for the document id.

Related

How can i render html with using geckoview?

I wanted to download a html file from a website first and edit that website and print that html with geckoview render engine
I know how to fetch a html and parse it but i don't know how to render it with using the renderengine
Geckoview just uses a url of a website
I don't want to use webview because i don't think it's not that good to make my own web browser app
GeckoSession has loadString(String htmlString, String mimeType) method:
GeckoSession geckoSession = ...;// here you start session in your GeckoView
geckoSession.loadString(yourHTMLString, "text/html");
geckoSession.loadData("My First Heading. My first paragraph.".getBytes(StandardCharsets.UTF_8),"text/html");
geckoSession.loadString("<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>","text/html") displays blank page. Awesome!

Webview doesnt load image with String Html

Im making an app in which i show one wikipedia page, for testing im create a new project.
mvw.getSettings().setJavaScriptEnabled(true);
mvw.getSettings().setLoadsImagesAutomatically(true);
mvw.getSettings().setAllowContentAccess(true);
mvw.getSettings().setDomStorageEnabled(true);
mvw.setWebViewClient(new mostrarEnAplicaion());
String htmlString = "<!DOCTYPE html><html><body style = \"text-align:center\"><img src=\"https://en.wikipedia.org/?curid=52633874#/media/File:Wat_Phra_Mahathat_Woramahawihan,_Nakhon_Si_Thammarat.jpg\" alt=\"pageNo\" height=\"100%\" width=\"100%\"></body></html>";
mvw.loadDataWithBaseURL(null,htmlString,"text/html","UTF-8","about:blank");
If I load the URL directly works fine
mvw.loadUrl("https://en.wikipedia.org/wiki/Santa_Mar%C3%ADa_del_Naranco");
I have the permission.INTERNET on the Manifest but doesnt work, anyone know the solution?
After look for a while, I just see that in this line
mvw.loadDataWithBaseURL(null,htmlString,"text/html","UTF-8","about:blank");
I only have to change the null (BaseUrl) for the url of the website in which the images are storage, in this case "https://en.wikipedia.org/"

How to open blob link use webview of android?

I want to display video in android app.
The link of video is blob link so that i think i need use webview to display it.
How to implement it?
WebView support loading HTML data from string. So following code is possible.
String videoBlobLinkUrl = "data:video/mp4;base64,[video base64-encoded data ..]";
String videoBlobMime = "video/mp4";
String videoHtml = "<html><body><video><source type=\""+videoBlobMime+"\" src=\""+ videoBlobLinkUrl +"\"></video></body></html>";
webview.loadData(videoHtml, "text/html", null);

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.

Is there any way to have WebView auto-link URLs and phone numbers in Android?

If a page has a URL or a phone number on it that isn't a link is there any way to have WebView recognize it and automatically turn it into a link like you can with TextViews?
With a TextView you would simply set the android:autoLink to the desired settings:
<TextView
android:autoLink="web|phone"
... />
but I can't find any equivalent for WebView.
If you are loading your own (web) content from a String, then you can do something like this:
final String content = "My email is: firstname#email.com ...";
Spannable sp = new SpannableString(content);
Linkify.addLinks(sp, Linkify.ALL);
final String html = "<body>" + Html.toHtml(sp) + "</body>";
myWebView.loadData(html, "text/html", "utf-8");
I don't know about any way which would make this work just by changing a setting, but a workaround would be to wait until the web page finishes loading and then do:
yourWebView.loadUrl("javascript:(function(){ /* code that creates links */ })()");
This will inject javaScript into the already loaded web page.
There's a slightly longer example available here: http://lexandera.com/2009/01/injecting-javascript-into-a-webview/.
You can find the JavaScript source for creating links if you take a look at the source of Linkify script for Greasemonkey (it's a plugin for Firefox in case you're not familiar with it). I believe it comes with the default install.

Categories

Resources