"no preview available" while viewing pdf in google docs in android webview - android

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

Related

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!

How can I load PDF files directly from Firebase Storage in Android WebView

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

How to load large pdf file in Android into my webview

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.

Webview doesn't show a specific website

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

How to display pdf document asynchronously in android?

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.

Categories

Resources