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.
Related
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!
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 making an Android App which has a feature that allows users to upload and view PDF files. And for the app, Firebase is the backbone. At the current stage of the app, the PDF files get uploaded to Firebase Storage. But I am having a hard time viewing it. I get the download URL from Firebase and use it in the method: webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + downloadURL) But the WebView shows me a grey background with "No Preview Available" written in the centre. I've opened the download URL on my browser and it displays the file perfectly (indicating that the file exists). How can i fix this issue?
just write one line above
pdf_url=getArguments().getString("pdf_url"); //Ur Firebase Url
progressBar=view.findViewById(R.id.course_content_progress);
webView=view.findViewById(R.id.pdfviewr);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
String url="";
try {
url=URLEncoder.encode(pdf_url,"UTF-8"); //Url Convert to UTF-8 It important.
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url="+url);
it's work for me.
Happy Coding
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.
I have included a web application within android web view , and there is a link in the webpage which opens some other site , when the link is clicked it works fine for the first click, however when clicked for the second time the website is not found ,
the code is :
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("some site ")) {
Intent i = new
Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
} else {
view.loadUrl(url);
return false;
}
}
#THelper and #mikegr, thanks for the reply,
Actually in my case i have a modal panel (JSF) in my web application which contains some buttons, on clicking the button i am opening some other site using javascript window.open() method which works fine in desktop browser, however, when i wrap this web application within android webview, everything works fine except when i first click this button i'm able to open the other site using the external browser, however on second click the webview tries to open this othersite within the webview instead of the external browser and i get website not found with the entire URL of the other site, this happens even when i logout and login again as the application launched is still running.
also in my case after sometime when the application is idle i get the black screen.
i surfed through the net and found simillar issue but that didn't help either , here is the link:
http://groups.google.com/group/android-for-beginners/browse_thread/thread/42431dd1ca4a9d98
handling links in a webview ,
any help and ideas would be very helpful for me, this is taking too long for me,
since i'm trying to display my web application in the web view, i have only one activity, which contains code like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
// so that when launcher is clicked while the application is
// running , the application doesn't start from the begnining
} else {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
// Show the ProgressDialog on this thread
this.progressDialog = ProgressDialog.show(this, "Pleas Wait..", "Loading", true);
browser = (WebView) findViewById(R.id.webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressDialog.isShowing()) {
progressDialog .dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("some site")) {
Intent i = new
Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
} else {
view.loadUrl(url);
return true;
}
}
});
browser.loadUrl("mysite");
}
}
I had the experience that shouldOverrideUrlLoading() is not called in certain circumstances.
There are a few bugs about this topic on
http://code.google.com/p/android/issues
like bug number 15827, 9122, 812, 2887
As a workaround try to add the method onPageStarted() and check if you get this call. For me this method is always called even if shouldOverrideUrlLoading() was not called before.
onPageStarted worked for me. Had to tweak it a bit, as that method is called when the webview is first rendered too, and I wanted to only execute it on the onClick of the banner's javascript.
I was simulating a banner with a custom page, so when the ad.html was being rendered, I avoided the startActivity. Otherwise, it launches the new browser window.
WebViewClient newWebClient = new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
if(!url.equals("http://xxxx.ad.html"))
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
};
Try to append the System parameter pattern in url before you load that, something like.
String url = "http://xxxx.ad.html?t="+System.nanoTime();
and in your shouldOverrideUrlLoading() method remove the query part (in a very first line).
int idx;
if ((idx = url.indexOf("?")) != -1) {
url = url.substring(0, idx);
}