PDF display in Android using WebView - android

I'm trying to display PDF in Android using WebView, I'm using google docs for this purpose. It was working fine till this morning. But all of a sudden it's throwing 502 error.
Is there any alternative to display PDF's in WebView.
This is the URL I have been using to render PDF in WebView
https://docs.google.com/gview?embedded=true&url=+URL
It looks like https://docs.google.com/gview?embedded=true&url= is down.

Try this way
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.endsWith(".pdf"))
{
// Load "url" in google docs
String googleDocs = "https://docs.google.com/viewer?url=";
mWebView.loadUrl(googleDocs + url);
}
else
{
// Load all other urls normally.
view.loadUrl(url);
}
return true;
}
UPDATE try this
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + url);

Related

How to intercept Android App Link into a WebView?

I'm trying to implement the following behavior into my Android App.
The app is a native app with some parts using WebView, in this WebView there is the possibility of using different payment methods.
If the user has the app of that bank installed I want to be able to open the corresponding app. In some cases, the website loaded into the WebView launch a specific intent:// that I can easily intercept and try to redirect the user to the app, but for the cases where the website use the new Android App link I'm not able to intercept them because they are normal https:// calls.
I tried to load the Android App link into Chrome and this way the app is opened.
My question at this point is how I can replicate the behavior of Chrome into my WebView??
I didn't find that much informations on this specific case besides the Android Documentation
Thanks for your help :)
You can intercept the url click by using custom WebViewClient for the webview.
1)Set the custom webview client for the webview
2)Override the below method and return true (you can do this based on particular url also. )
shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
3)Override below method and handle the click and start the activity using either package name or playstore url with intent.
public void onLoadResource (WebView view,
String url)
WebView mWebView;
WebViewClient customWebClient = new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
//This can be modified based on the url . URL can be retrieved using request.getUrl()
return true;
}
#Override
public void onLoadResource(WebView view, String url){
if( url.equals("") ){
// launch playstore activity
final String appPackageName = "package name of the application"
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
}
}
mWebView.setWebViewClient(customWebClient);

Android view .pdf file in web view using redirect url by authenticate user name and password

I would like to view pdf file from share point server.
the Url link : https:///sites/iConnect/Lists/FeedList/Attachments/1138/Grasim%20Talent%20Hunt.pdf
it Redirect to the below link:
https://login.microsoftonline.com, once I signed in then the response redirect to pdf file url.
In this institution how I handle and view the pdf file to webview.
I tried this code snip:
WebView webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(uri);
webView.setWebViewClient(new WebViewClient()
{
#Override
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
{
if(request.getUrl().toString().endsWith(".pdf"))
{
String weburl = "https://docs.google.com/gview?embedded=true&url=" + request.getUrl().toString();
view.loadUrl(weburl);
}
else
{
view.loadUrl(request.getUrl().toString());
}
return true;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
CommonUtils.dismissDialog();
}
#Override
public void onReceivedError(WebView view, int errorCode,String description,String failingUrl) {
//super.onReceivedError(view, request, error);
Toast.makeText(WebViewActivity.this,"Error"+description, Toast.LENGTH_SHORT).show();
}
});
Please suggest if there is any concepts
Thank you
Rajarajan
I used onPageStarted instead of shouldOverrideUrlLoading and didn't have any problem.
and i user webView.loadUrl(uri) instead view.loadUrl(weburl). try that.
WebView cannot display a pdf file.You can display the pdf by using below code
if(url.endsWith(".pdf")){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
else {
view.loadUrl(url);
}
If you're OK with rendering the PDF outside a WebView (e.g., an ImageView), and you're targeting Lollipop (5.0) and up, you can use this:
https://developer.android.com/reference/android/graphics/pdf/PdfRenderer
I was actually trying it out today and it worked well for me. What I did was:
Create a Fragment with an ImageView and then two buttons (one for back one page, one for forward one page)
Each time the user when forward or back, I created a Bitmap with Bitmap.createBitmap(w, h, Config.ARGB_8888) where w and h are the ImageView's width/height at that point
I requested the given page number and passed in the new Bitmap where the mBitmap is in the sample code
I assigned the resulting Bitmap with imageView.setImageBitmap(bitmap).
Voila, it worked well. My code is a prototype, so I'll be refining it, but it rendered dozens of pages well and was relatively quick. There was a noticeable delay b/w pages, but maybe 1/2 second worth. If I had pre-loaded / cached pages, the delay would probably be gone. But, for my prototype, it didn't matter.
I'd highly recommend this approach!

Display a pdf with dynamic generating url in WebView / Google Docs

so I just started with Android programming and I am trying to make a little app using WebView. There is a url that redirects you to a pdf, I know WebView does not render pdf. So I want to use intent and display the pdf in Google Docs. However, the pdf address is randomly generated so I cant link it with
WebView.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdfURL);
How can I send an intent to Google Docs without using the exact pdf address?
I don't know what "randomly generated" means.
But the first thing that comes to my mind is to set a WebViewClient and override shouldOverrideUrlLoading:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".pdf") == true) {
view.loadUrl("http://docs.google.com/gview?embedded=true&url=" + url);
return true;
}
return false;
}
});
Some more info in this thread.

webview shouldOverrideUrlLoading doesn't work as expected?

I have a webview that loads a real URL something like https://example.com
I would like to open some links according to a pattern in a second webview.
I have the following code:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Handle external url
boolean openInNew = manageUrl (url);
Log.i("openurl", url + " : " + openInNew);
if (openInNew) {
this.callBack.openWebView2 (url);
return true;
} else {
return false;
}
}
The second webview shows correctly the external url I want to load.
The problem I found is about what happens with the original webview, it is also redirected to an empty blank page (with a loading icon).
Is possible to keep the first webview in the original page? I thought that with shouldOverrideUrlLoading should be enough.
Maybe I forgot something, any help would be appreciate.
Thanks a lot.

Android WebView loading part of a website

Some websites have comments portion and many other unnecessary portions. I want to just display the main article excluding the other portions in my custom Android WebView.
I have tried the example from Display a part of the webpage on the webview android
But it still shows the full website.
There is also the loaddatawithbaseurl class in Android but it is not working too as you can't specify till which portion of a website to show using this class.
Is there any other way to do this?
Here it is with some changes...
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
String javaScript = "javascript: var form = document.getElementsByClassName('university_corner');"
+ "var body = document.getElementsByTagName('div');"
+ "body[0].innerHTML = form[0].innerHTML";
webView.loadUrl(javaScript);
}
});
webView.loadUrl(url);

Categories

Resources