Reputation: 11
Hi I try to display pdf with webview. I set all codes properly but pdf didnt open. but I give a webpage link it works good. what is my mistake I am newbie sorry.
WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_giris);
wv = (WebView) findViewById(R.id.wv);
WebSettings webSettings = wv.getSettings();
wv.setWebViewClient(new WebViewClient());
wv.getSettings().setBuiltInZoomControls(true); //zoom yapılmasına izin verir
wv.getSettings().setSupportZoom(true);
// wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setJavaScriptEnabled(true);
materialDesignFAM = (FloatingActionMenu) findViewById(R.id.social_floating_menu);
floatingActionButton1 = (FloatingActionButton) findViewById(R.id.floating_facebook);
floatingActionButton2 = (FloatingActionButton) findViewById(R.id.floating_twitter);
floatingActionButton3 = (FloatingActionButton) findViewById(R.id.floating_linkdin);
floatingActionButton4 = (FloatingActionButton) findViewById(R.id.floating_google_plus);
floatingActionButton5 = (FloatingActionButton) findViewById(R.id.floating_instagram);
floatingActionButton6 = (FloatingActionButton) findViewById(R.id.floating_youtube);
floatingActionButton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String fullPath = "http://aciltip.medicine.ankara.edu.tr/files/2015/09/2015_d45_acil_tip_hizmetleri.pdf";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullPath));
startActivity( browserIntent);
// wv.loadUrl("https://docs.google.com/gview?embedded=true&url=http://aciltip.medicine.ankara.edu.tr/files/2015/09/2015_d45_acil_tip_hizmetleri.pdf");
// String url = "http://www.example.com";
// startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
floatingActionButton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
wv.loadUrl("http://docs.google.com/viewer?embedded=true&url=http://www.ctf.edu.tr/stek/pdfs/70/7001.pdf");
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
}
});
floatingActionButton3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
wv.loadUrl("http://docs.google.com/gview?embedded=true&url=http://www.ctf.edu.tr/stek/pdfs/70/7001.pdf");
}
});
and logcat is these:
03-07 08:50:26.577 3489-3547/com.er.ap D/EGL_emulation: eglCreateContext: 0xa3539fa0: maj 2 min 0 rcv 2 03-07 08:50:26.578 3489-3547/com.er.ap D/EGL_emulation: eglMakeCurrent: 0xa3539fa0: ver 2 0 03-07 08:50:27.446 3489-3489/com.er.ap I/chromium: [INFO:CONSOLE(0)] "The Content-Security-Policy directive 'base-uri' is implemented behind a flag which is currently disabled. ", source: https://accounts.google.com/o/oauth2/postmessageRelay?parent=https%3A%2F%2Fdocs.google.com&jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en_US.dLR0UQgpDEo.O%2Fm%3D__features__%2Fam%3DAQE%2Frt%3Dj%2Fd%3D1%2Frs%3DAGLTcCMdWrzahDTQIubih7dySWJqcBU_nw#rpctoken=580516055&forcesecure=1 (0)
But below works good as my will but it quits my apps and go to the pdf url and my back button disapper. if anyone help me this issue. it will be very usefull for me
floatingActionButton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String fullPath = "https://drive.google.com/open?id=0B16f9Z9VbJmCc3RhcnRlcl9maWxl";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullPath));
startActivity( browserIntent);
});
Upvotes: 0
Views: 925
Reputation: 1767
Please you the following code
public class WebSample extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
WebView mWebview = (WebView) findViewById(R.id.web);
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
});
String pdf = "http://aciltip.medicine.ankara.edu.tr/files/2015/09/2015_d45_acil_tip_hizmetleri.pdf";
mWebview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
}
Upvotes: 0
Reputation: 2265
Use this code helps, you may download or view the file by two ways like in-built browser or in the webview :-
floatingActionButton3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://docs.google.com/gview?embedded=true&url=http://www.ctf.edu.tr/stek/pdfs/70/7001.pdf"));
startActivity(browserIntent);
}
});
for webview
:-
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "http://aciltip.medicine.ankara.edu.tr/files/2015/09/2015_d45_acil_tip_hizmetleri.pdf";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
Upvotes: 0
Reputation: 1112
Try enabling JavaScript.
Webview webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(pdf_url);
Upvotes: 0
Reputation: 1474
You need to append the pdf url to Google Doc Viewer that is where you went wrong
so i created seperate variable to store pdf url and then pass it to viewer see my below code
floatingActionButton3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String pdf = "http://www.ctf.edu.tr/stek/pdfs/70/7001.pdf"; //your pdf address
wv.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf); //simply append pdf address to doc viewer
}
});
Upvotes: 0
Reputation: 1731
Try this,
webview = (WebView) findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://docs.google.com/viewer?embedded=true&url=http://www.ctf.edu.tr/stek/pdfs/70/7001.pdf");
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Upvotes: 1
Reputation: 1805
Solution:-
You can open PDF in Google Docs Viewer by appending URL to:
http://docs.google.com/gview?embedded=true&url= This would open PDF in default browser or a WebView.
You can use this format.
https://docs.google.com/viewerng/viewer?url=http://yourfile.pdf
Just replace http://yourfile.pdf with the link you use.
Upvotes: 0