Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何打开pdf?_Android - Fatal编程技术网

Android:如何打开pdf?

Android:如何打开pdf?,android,Android,我试图打开pdf,但当我按下按钮时,什么也没发生。 我的错在哪里 OnClickListener oclBt2 = new OnClickListener(){ public void onClick(View v) { File file = new File("http://testserv1.p.ht/1/ksu016.pdf"); if (file.exists()) {

我试图打开pdf,但当我按下按钮时,什么也没发生。 我的错在哪里

OnClickListener oclBt2 = new OnClickListener(){
            public void onClick(View v) {
                File file = new File("http://testserv1.p.ht/1/ksu016.pdf");

                if (file.exists()) {
                Uri path = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);   
                try {
                    startActivity(intent);
                    } 
                catch (ActivityNotFoundException e) {
                     e.printStackTrace();
                    }
                }
            }
        };
我更正了我的代码,但它不再起作用:(当我按下按钮时,出现了(对不起,但我的声誉不允许发布图像)


首先,
File
用于本地文件,而不是
http
urlhttp://testserv1.p.ht/1/ksu016.pdf”;获取指向
http
URL的
Uri


其次,可能没有设置为直接从HTTP URL下载的PDF查看器。为了更大的兼容性,您可以先安排下载PDF(使用
DownloadManager
或您自己的HTTP客户端代码),然后查看本地PDF文件。

首先,
文件
用于本地文件,而不是
http
URL。使用
Uri.parse(“http://testserv1.p.ht/1/ksu016.pdf”;
获取指向
http
URL的
Uri


其次,可能没有设置为直接从HTTP URL下载的PDF查看器。为了更大的兼容性,您可以先安排下载PDF(使用
DownloadManager
或您自己的HTTP客户端代码),然后查看本地PDF文件。

@RostislavShtanko:您没有安装能够处理
http://
URL的PDF查看器。我从未尝试过从
docs.google.com
查看内容,因此我无法对失败的原因发表评论,除了您可能缺少
INTERNET
权限之外。INTERNET没问题。我没有取消发布关于PDF阅读器,我想是的installed@RostislavShtanko:同样,您可能安装了一个,但它不知道如何直接处理
http
URL。通常,当用户在Web浏览器中单击指向PDF的链接时,浏览器会下载该文件,然后启动PDF查看器以查看本地文件。我下载了DownloadManager,现在一切都好了。首先,文档正在下载,然后打开。感谢您的支持helping@RostislavShtanko:您没有安装可处理
http://
URL的PDF查看器。我从未尝试过从
docs.google.com
查看内容,因此我无法对失败的原因发表评论呃,也许你没有
互联网
权限。互联网没问题。我不理解PDF查看器。我想,它installed@RostislavShtanko:同样,您可能已经安装了一个,但它不知道如何直接处理
http
URL。通常,当用户在Web浏览器中单击指向PDF的链接时,浏览器会浏览er下载文件,然后启动PDF查看器查看本地文件。我下载了DownloadManager,现在一切正常。首先,文档正在下载,然后打开。感谢您的帮助
OnClickListener oclBt2 = new OnClickListener(){
            public void onClick(View v) {
                Uri path = Uri.parse("http://testserv1.p.ht/1/ksu016.pdf");
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                startActivity(intent);
                } 
                catch (ActivityNotFoundException e) {               
                mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url=http://hostforandroid.elitno.net/pdf-test.pdf" );
                setContentView(mWebView);
                }               
            }
        };