0

我是 android 应用程序开发新手。我正在尝试打开存储在 sd 卡中的 pdf 文件。我使用了这个代码片段,但这会打开设备中的查看器,但不会打开路径中给出的文件。任何帮助表示赞赏。

    Intent intent = new  Intent(Intent.ACTION_VIEW,Uri.parse("Environment.getExternalStorageDirectory().getPath()+/sample.pdf"));
    intent.setType("application/pdf");
    intent.setPackage("com.adobe.reader"); selects the adobe reader directly 
    PackageManager pm = getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(intent,0);
    if (activities.size() >= 0) 
            {
    startActivity(intent);


    } else {
    Toast.makeText(this, "No Application Available to View PDF",

        Toast.LENGTH_SHORT).show();
    }
4

2 回答 2

1

您可以通过提供路径从 sdcard 读取 pdf 文件并尝试以下操作。

File pdfFile = new File(path); 
if(pdfFile.exists()) 
{

    Uri path = Uri.fromFile(pdfFile); 
    Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
    pdfIntent.setDataAndType(path, "application/pdf");
    pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    try
    {
        startActivity(pdfIntent);
    }
    catch(ActivityNotFoundException e)
    {
        Toast.makeText(uractivity.this, resource.getString(R.string.noapplicationpdf), Toast.LENGTH_LONG).show(); 
    }
}
于 2012-05-25T02:48:27.207 回答
0

你可以尝试类似的东西

Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(filename);
intent.setDataAndType( Uri.fromFile( file ), "application/pdf" );
startActivity(intent);

或者

Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);
于 2012-05-25T02:25:43.573 回答