Reputation: 77
Around 2 days I tried to open PDF files in my custom WebvView
. Here's my WebView
code:
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class WebActivity extends AppCompatActivity {
public
WebView mywebViewfull;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
String url = getIntent().getStringExtra("url");
mywebViewfull = (WebView) findViewById(R.id.webnav1);
progressBar=(ProgressBar)findViewById(R.id.progressbar1);
WebSettings webSettings= mywebViewfull.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setGeolocationEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setSaveFormData(true);
webSettings.setAppCacheEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(false);
webSettings.setGeolocationDatabasePath(getFilesDir().getPath());
webSettings.setAllowFileAccess(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
mywebViewfull.loadUrl(url);
mywebViewfull.setWebViewClient(new mywebViewfullClient());
}
@Override
public void onBackPressed() {
if(mywebViewfull.canGoBack())
{
mywebViewfull.goBack();
}
else
{
super.onBackPressed();
}
}
class mywebViewfullClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// android.support.v7.app.ActionBar actionBar = getSupportActionBar();
// actionBar.hide();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("http://")) {
//open url contents in webview
return false;
}if (url != null && url.startsWith("https://")) {
//open url contents in webview
return false;
//this must open pdf file
}if (url.endsWith(".pdf")) {
mywebViewfull.loadUrl("https://docs.google.com/gview?embedded=true&url="+getIntent().getStringExtra("url"));
return false;
}
else {
//external links in app
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
//back previous page
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction()==KeyEvent.ACTION_DOWN){
switch (keyCode){
case KeyEvent.KEYCODE_BACK:
if (mywebViewfull.canGoBack()) {
mywebViewfull.goBack();
}
else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
So... When I try to open pdf document, nothing happens in my app.
I can't see what's wrong! Can anyone help me?
Upvotes: 6
Views: 21788
Reputation: 298
If you want to display the PDF inside the web view use:
url = "https://docs.google.com/viewer?url=https://linkToYourPdf.pdf"
webView.loadUrl(url);
If your PDF contains more then one page, don't forget to enable the vertical scroll
webview.setVerticalScrollBarEnabled(true);
Upvotes: 2
Reputation: 2707
Try the following for the webview option if you choose that
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "online pdf link";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
But Remember if you use google docs for viewing the pdf or any other doc, google docs may throw you the error stating
You've reached the bandwidth limit for viewing or downloading files that aren't in Google Docs format
..... So doesn't seem reliable
So, be cautious with google doc viewing
please refer to my whole answer if you have a pdf reading through asset folder
Upvotes: 1
Reputation: 3149
There is no native support to open PDF files in Android, but you have some workarounds that can more or less do the job for you. Alternatives:
1 - You could open your system default app to read the pdf file by an intent from your app;
2 - You could use a third party library to open the pdf inside your app;
3 - You could invoke Google Docs website functionality to open your pdf file inside your webview. To do so, use the following code sample:
String pdfUrl = "http://yourwebsite.com/yourfile.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + pdfUrl;
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
Upvotes: 13