I am displaying a pdf document in my android application. For that I followed from the link How to open a PDF from an Android app (in a separate PDF viewer app). My pdf document size is of 30mb. So it is taking time to display it. Hence I need to display it asynchronously. I am new to asynchronous tasks. Please give me some idea on how to display the pdf asynchronously.
use ProgressDialog as follow
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
progressDialog = ProgressDialog.show(Activity_PDF.this, "Loading",
"Please wait", true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(Activity_PDF.this, description,
Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
super.onPageFinished(view, url);
}
});
// used to read PDF files from docs.google.com
webview.loadUrl("http://docs.google.com/gview?embedded=true&url="
+ stPdfLink);
First of all, it's not you the one who open this pdf. You open it with other application, so if rendering speed is low, the only thing you can do is select other app for openeing it. It's not your responsibility and in your case it's not related to asynchronous tasks at all. I think it will be usefull for you to read about Android Fundamentals first of all and get some more knowlege abour Android framework and how it works.
Related
While opening pdf in webview in android with google doc links :-
webView.loadUrl("http://docs.google.com/gview&embedded=true&url=" + getIntent().getStringExtra(CONSTANT.pdfurl));
for some pdf
"no preview availbale"
happens in webview
, and for some pdf it always happen , i know this question have been asked several times and have seen all the stackoverflow and internet but could not find any satisfactory explanation to it.
How to know when "no preview available" happens while viewing pdf in google docs and how to solve this problem, also
the progress bar stops automatically without showing any content
sometimes
here is my full code of implementation :-
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
progressBar.setVisibility(View.VISIBLE);
webView.loadUrl("http://docs.google.com/gview&embedded=true&url=" + getIntent().getStringExtra(CONSTANT.pdfurl));
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
// do your stuff here
progressBar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
view.loadUrl("about:blank");
Toast.makeText(getApplicationContext(), "Error occured, please check newtwork connectivity", Toast.LENGTH_SHORT).show();
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
One thing i know for sure is that in http website it happen more frequently than https website . How to resolve this issue ?
Is there any way to covert url to https from http without changing website ?
check your pdf path may be it will null like (https://docs.google.com/gview?embedded=true&url=null)
Or use intent to open pdf I found it easy
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
One of the reason's of showing,
"no preview available"
while loading PDF in WebView is the bandwidth limit that Google Doc's imposes for every request made to it.
As per their documentation , To help keep our systems healthy and your account safe, all Google Apps accounts have a limited amount of bandwidth allocated to it`
For more insight's check out the below links ,
1. https://support.google.com/a/answer/1071518?hl=en
2. Reaching the bandwidth limit for viewing pdf files in WebView through Google docs
i am unable to load a large pdf file (having 900 page) into my
webview in my android app, i try this code and working well on any
other pdf, but when i try to open a large one it display: No Preview
Available.
wvReport.getSettings().setJavaScriptEnabled(true);
wvReport.getSettings().setAllowFileAccess(true);
wvReport.getSettings().setAllowContentAccess(true);
wvReport.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
Toast.makeText(ReportsViewActivity.this, "Oh no! " , Toast.LENGTH_SHORT).show();
}
});
wvReport.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int progress) {
if (progress == 100) {
progressBar.setVisibility(View.INVISIBLE);
progressBar.setProgress(0);
} else {
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(progress);
}
}
#Override
public void onReceivedTitle(WebView view, String title) {
}
});
wvReport.loadUrl("http://docs.google.com/viewer?url="+url);
Try this code:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
and it's working, so if the pdf is too large , will be downloaded and opened via pdf viewer.
You don't. Webviews display html. The fact that this works at all is by a hack- google will convert simple pdfs into html. It doesn't seem like they support anything that big. Even if they did, I would expect loading a 900 page pdf converted to html would be so large I highly doubt you'd be able to load it without going OOM. Use an apropriate pdf library, make a real pdf rendering view, and make sure not to render more of the pdf at a time than you need (or else you'll go OOM anyway). In other words, don't rely on hacky solutions you never should have relied on in the first place.
You should try alternatives like PDF.js running locally in your device, instead of a service like Google Docs preview.
Put it in your assets folder and tweak the example:
wv.loadUrl("file:///android_asset/web/viewer.html");
Also, as #gabe-sechan mentions, you can have Out Of Memory situations. An alternative to try is a native viewer like AndroidPdfViewer.
I'm trying to do a webview based application for this website to show it in a mobile application.
when I put any different site the application work great, but in this specific site never show the second page when I clicked in the button to go to the desk page. However, I put a Log statement in the onPageFinished method and log that the page is loaded completely.
My Code Here
final WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getActivity(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d("WEBSITE", "Page Loaded.");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("WEBSITE", url);
myWebView.loadUrl(url);
return false;
}
});
myWebView.loadUrl("https://demo.frappecloud.com/");
I believe the problem is with your shouldOverrideUrlLoading. If you check the documentation you will see that :
This method is not called for requests using the POST "method"
When you are submitting the Form, you are making a POST request, which is basically ignored.
Here is the link: WebViewClient
Also check this reference where it says how you could load a URL with POST data: LOAD A POST REQUEST INTO A WEBVIEW IN ANDROID
Consider checking this thread: Android - how to intercept a form POST
I am using an authenticated webview in my app. Currently when we enter the search term in the search box,it is added to the url (say http.cookies.com/something/SEARCHTERM/something) and the url is called in the webview which loads the url as follows:
if(!StringUtils.isEmpty(URL)){
if(URLUtil.isValidUrl(URL)){
mWebview.loadUrl(URL);
}else{
mWebview.loadUrl("http://www.google.com");
}
However, even if the url is invalid, it still loads the invalid url which shows up as "412- precondition set and hence page cannot be displayed".Is there a way I can detect if this search term or url is valid and display it in the webview , if valid) as the requested url , if not valid, i display some other webview like say google.com?
Thanks!
justin
You can use the Webview's WebViewClient to catch the error and do whatever you see convenient. Here is a sample:
yourWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
I've searched a lot for this but did not find any answers. I am developing an android app in which at some point, a webview is displayed which shows you a webpage. But I am really worried about a small advertisement on the web view which shows porn content. Is there any way I can block that from loading on the webpage? Every resource passes the onLoadingRecource() method...Is this the place where i can find a solution? I really need help. Thank you.
Since API 11, there's WebViewClient.shouldInterceptRequest, here you can catch loading of embedded objects (images, etc) and replace it with your own image.
For example:
WebResourceResponse wr = new WebResourceResponse("", "", new FileInputStream("/sdcard/aaa.jpg"));
return wr;
You must detect yourself what you want replace by what.
On API<11 it may be more complicated to achieve this (I don't know yet how).
You can remove any Element from the page by injecting JavaScript into a WebView. Below is an example of how to inject JavaScrpt into a WebView to remove an element having its id:
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
// Removes element which id = 'mastHead'
view.loadUrl("javascript:(function() { " +
"(elem = document.getElementById('mastHead')).parentNode.removeChild(elem); " +
"})()");
}
You can use the below code to check whether to load it on not. Where webview is the object for WebView.
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
Toast.makeText(activity,"onPageStarted url :"+url, Toast.LENGTH_LONG).show();
}
#Override
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
Toast.makeText(activity,"Connecting url :"+url, Toast.LENGTH_LONG).show();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
I think this will help you.
The method has 2 params, override it in your webview and discard urls starting with the domain you want to avoid.