1

I try to open pdf, but when I press the button, nothing happens. Where is my mistake?

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();
                    }
                }
            }
        };

I corrected my code, but it doesn't work again :( when I press the button, appears the window(Sorry, but my reputation doesn't allow to post images)

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);
                }               
            }
        };
4

1 回答 1

1

首先,File是针对本地文件,而不是httpURL。用于Uri.parse("http://testserv1.p.ht/1/ksu016.pdf");获取Uri指向httpURL。

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

于 2013-06-06T15:40:12.590 回答