简体   繁体   English

如何在没有下载选项的情况下在 Android 中在线从 url 打开 pdf?

[英]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.在我的应用程序中,我希望用户阅读我的应用程序中的 pdf 文件,而无需任何下载选项。 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.您可以在 Android 中在线从 URL 打开 pdf,无需下载选项。

Kotlin 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 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这将简单地打开设备的 pdf 查看器应用程序,几乎所有 android 设备com.google.android设备中都有。

You can also safeguard this code by querying packages beforehand and adding fallbacks您还可以通过预先查询包并添加回退来保护此代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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