如何在android中从服务器打开PDF文件

发布于 2024-12-22 05:43:34 字数 273 浏览 3 评论 0原文

我有一个要求,其中有一个 URL = "http://www.example/Open.pdf"

现在,我想从我的 Android 应用程序直接打开这个 PDF 文件在默认的 PDF 查看器中。

当我点击网页上的此链接时,用户应该会看到一个使用此文档打开的默认 PDF 查看器。

注意:此文件不应存储在 SD 卡上。

我该如何继续此实施?

I have a requirement where there is a URL = "http://www.example/Open.pdf"

Now from my android application I want to open this PDF file directly in the default PDF viewer.

The moment I click on this link on the webpage, user should be presented with a default PDF viewer opened with this document.

Note: This file should not be stored on the SD card.

How do I proceed for this implementation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无远思近则忧 2024-12-29 05:43:34

我们可以在 webview 中打开 PDF 文件而不缓存它。在"onCreate"方法中编写以下代码。

工作代码:

    String url = "http://www.example.com/abc.pdf";
    final String googleDocsUrl = "http://docs.google.com/viewer?url=";


    WebView mWebView=new WebView(this);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginsEnabled(true);

    mWebView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url){

            view.loadUrl(url);
            return false; // then it is not handled by default action
       }
    });


    mWebView.loadUrl((googleDocsUrl + url));

    setContentView(mWebView);

这里发生的是您使用 Google Docs 打开 PDF。使用上述方法的最大优点PDF的延迟加载。 PDF 有多重并不重要。 Google 文档会处理它。

We can open PDF file in the webview without caching it. Write below code in "onCreate" method .

Working code :

    String url = "http://www.example.com/abc.pdf";
    final String googleDocsUrl = "http://docs.google.com/viewer?url=";


    WebView mWebView=new WebView(this);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginsEnabled(true);

    mWebView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url){

            view.loadUrl(url);
            return false; // then it is not handled by default action
       }
    });


    mWebView.loadUrl((googleDocsUrl + url));

    setContentView(mWebView);

What happens here is you open the PDF using Google Docs. Best Advantage of using above method is the lazy loading of PDF. Does not matter how heavy the PDF is. Google Docs takes care of it.

硬不硬你别怂 2024-12-29 05:43:34

您可以使用 googleDocsWebView 中查看 pdf。

WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");

You can view the pdf in the WebView using googleDocs.

WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");
旧时浪漫 2024-12-29 05:43:34

您无法从应用程序中打开默认的 PDF 视图。

如果您的文件位于服务器上并且您想在不下载的情况下打开它,那么这也可能会带来更大的安全问题。如果像默认的 adobe reader 这样的外部应用程序可以访问您服务器上的内容,那么这就完全破坏了安全框架。

因此,最好的选择是启动浏览器或 webview 的新实例,并向用户显示 google 文档中的 PDF 文档。

这样用户就可以阅读文档并返回到应用程序的最新状态。

There is no way you can open a default PDF view from your application.

If your file is on the server and you want to open it without downloading then this might also pose a greater security concern. If external applications like default adobe reader can access the content on your server, then this is breaking the security framework altogether.

So, best option would be to launch a new instance of browser or webview and show the PDF document in google docs to the user.

This way user can read the document and get back to the recent state of the application as well.

鲜肉鲜肉永远不皱 2024-12-29 05:43:34

<块引用>

您可以使用 googleDocsWebView 中查看 pdf。

WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");

除了使用 http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf

You can view the pdf in the WebView using googleDocs.

WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");

do you have the others solution besides view pdf file using http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文