简体   繁体   中英

How to open pdf from url online in Android without download option?

In my app I want the user to read the pdf files inside my app without any option to download them. How can I do this? I am using the following code but not working-

WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.loadUrl("https://docs.google.com/viewer?"+pdf_url);

Is there any other way to do this? If you can do it?

You can open pdf from URL online in Android without a download option.

Kotlin

val path="https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf"
    
activityWebViewBinding.webview.settings.loadWithOverviewMode = true
activityWebViewBinding.webview.settings.javaScriptEnabled = true
val url = "https://docs.google.com/gview?embedded=true&url=$path"
activityWebViewBinding.webview.loadUrl(url)

Java

 String path="https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf";
        
 webview.settings.setLoadWithOverviewMode(true);
 webview.settings.setJavaScriptEnabled(true);
 String url = "https://docs.google.com/gview?embedded=true&url="+path;
 webview.loadUrl(url);

You can simply launch intent as below

val pdfUrl = "ENTER_PDF_URL_HERE"
val intent: Intent = Intent(Intent.ACTION_VIEW, Uri.parse(pdfUrl))
    .addCategory(Intent.CATEGORY_BROWSABLE)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)

This would simply open it up device's pdf viewer app which comes in almost all android devices com.google.android.apps.docs

You can also safeguard this code by querying packages beforehand and adding fallbacks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM