1

可能重复:
在 Android 中打开 PDF

我想从我的 android 应用程序中用 Adob​​e Reader 打开一个 PDF 文件。

我在 /mnt/sdcard 中有一个名为 test.pdf 的 PDF 文件,我正在使用下一个代码:

file = new File ("/mnt/sdcard/test.pdf");

if (file.exists())
{
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
    intent.setType("application/pdf");
    intent.setPackage("com.adobe.reader");          
    startActivity(intent);              
}

执行此代码时打开 Adob​​e Reader,但未打开文件。该文件有效。

问题是什么?

4

2 回答 2

6

试试这个。

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);
于 2012-11-20T08:55:19.823 回答
1

试试这个代码

File file=new File("/mnt/sdcard/test.pdf");
if(file.exists())
{
    Uri path=Uri.fromFile(file);
    Intent intent=new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(path, "application/pdf");

    try
    {
        startActivity(intent);
    }
    catch(ActivityNotFoundException e)
    {
        Toast.makeText(TestActivity.this, "No software for PDF", Toast.LENGTH_SHORT).show();
    }
}
于 2012-11-20T08:51:58.477 回答