繁体   English   中英

在Webview意向中在Android应用中查看PDF(从URL)

[英]Viewing PDF (from URL) in android app within a webview intent

我正在尝试从设备/仿真器远程查看PDF文件。 我一直在做大量研究,并一直在研究stackoverflow上如何做到这一点,而不必下载PDF,然后以这种方式查看文件。

这是尝试执行此操作的相关代码段:

if (url.contains("CreateQuoteDocument")) {
    webview.getSettings().setJavaScriptEnabled(true);
    try {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(url),"application/pdf");
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Log.e("something went wrong", e.getMessage());
    }
}

我收到以下异常:

E/something went wrong: No Activity found to handle Intent { act=android.intent.action.VIEW ...

因此,我有两个问题,

  1. 为什么这不起作用?

  2. 还有另一种方法吗? (而不使用Google文档时)

编辑

该网址已使用http:// www正确格式化

尝试这个:

String myPdfUrl = "http://example.com/awesome.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + myPdfUrl;
Log.i(TAG, "Opening PDF: " + url);
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl(url);

为什么这不起作用?

您尚未安装包含支持该Intent结构的活动的application/pdf (例如, url碰巧使用的任何方案均为application/pdf )。

还有另一种方法吗?

您可以安装包含支持该Intent结构的活动的应用。 当然,并非所有用户都会有这样的应用程序,但是也许您只是打算个人使用代码,在这种情况下,除了您之外,没有其他用户。

暂无
暂无

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

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