在android模拟器上打开外部PDF文件

时间:2014-05-12 22:44:58

标签: java android pdf android-intent android-emulator

我尝试打开具有外部链接的PDF文件,但在Adobe阅读器打开后,它会显示“文档路径无效”

这是我写的代码:

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                File file = new File("http://www.kb.nl/sites/default/files/docs/pdf_guidelines.pdf");
                                Uri path = Uri.fromFile(file);
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                intent.setDataAndType(path,"application/pdf");
                                try 
                                {
                                    startActivity(intent);
                                } 
                                catch (ActivityNotFoundException e) 
                                {
                                    Toast.makeText(MainActivity.this, 
                                        getString(R.string.app_name), 
                                        Toast.LENGTH_SHORT).show();
                                }
            }
        });

如何在模拟器中打开外部PDF文件,我的目的是打开google驱动器中的pdf文件。

提前致谢。

2 个答案:

答案 0 :(得分:0)

File file = new File("http://www.kb.nl/sites/default/files/docs/pdf_guidelines.pdf");

http://www.kb.nl/sites/default/files/docs/pdf_guidelines.pdf不是File的路径。它是网站的URL。您无法将Web URL包装在File对象中并希望它能够正常工作。

您可以尝试使用Uri.parse("http://www.kb.nl/sites/default/files/docs/pdf_guidelines.pdf")获取Uri,看看Adobe Reader是否会支持,但我对此表示怀疑。

更有可能的是,您需要自己下载PDF,然后在File中使用 Intent

答案 1 :(得分:0)

这几条线做了魔术:

try {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("Url to pdf or other")));
            } catch (ActivityNotFoundException e) {
                //do something here
            }