在Android中显示PDF

7
在我的onCreate()方法中,我进行了如下检查:
//
// check if we have a PDF viewer, else bad things happen
//
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("application/pdf");

List<ResolveInfo> intents = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

if (intents == null || intents.size() == 0) {
       // display message then...
       finish();
}

在我的HTC Desire手机上,即使我已经安装了Adobe的PDF阅读器,这段代码也无法返回匹配结果。一个关于如何在Android应用程序中使用内置PDF阅读器打开PDF文档的问题回答提到,Adobe可能没有任何公共意图(Intent),因此上述检查显然会返回空值。
有人能否验证是否可以通过意图启动Acrobat,或者是否有其他方法或PDF阅读器可供使用。
实际使用情况是下载发票副本并使用以下代码将其存储在本地存储中:
 URL url = new URL(data);
 InputStream myInput = url.openConnection().getInputStream();

 FileOutputStream fos = openFileOutput(fname, Context.MODE_WORLD_READABLE);

 // transfer bytes from the input file to the output file
 byte[] buffer = new byte[8192];
 int length;
 while ((length = myInput.read(buffer)) > 0) {
    fos.write(buffer, 0, length);
    progressDialog.setProgress(i++);
 }
 fos.close();

然后展示

// read from disk, and call intent
openFileInput(fname);   // will throw FileNotFoundException

File dir = getFilesDir();       // where files are stored
File file = new File(dir, fname);   // new file with our name

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
intent.setType("application/pdf");

startActivity(intent);
2个回答

6
连接手机和电脑,启动Eclipse并打开LogCat。然后使用浏览器下载PDF文件并打开它。你应该能看到类似这样的一行(我使用的是HTC Desire):
09-14 17:45:58.152: INFO/ActivityManager(79): Starting activity: Intent { act=android.intent.action.VIEW dat=file:///sdcard/download/FILENAME.pdf typ=application/pdf flg=0x4000000 cmp=com.htc.pdfreader/.ActPDFReader }
尝试使用组件信息进行显式意图。文档在此处说明:

> component -- 指定用于意图的组件类的显式名称。通常,这是通过查看意图中的其他信息(操作、数据/类型和类别)并将其与可以处理它的组件匹配来确定的。如果设置了此属性,则不执行任何评估,而是完全使用此组件。通过指定此属性,所有其他Intent属性变为可选。

缺点是你将受限于HTC阅读器。但你可以先尝试隐式意图,如果失败了再作为后备方案尝试显式意图。

0

-将以下代码复制到您的活动中。从onCreate()函数调用CopyReadAssets("File_name.pdf")函数。将File_name.pdf文件放置在资产文件夹中。

private void CopyReadAssets(String pdfname)
{
    AssetManager assetManager = getAssets();
    InputStream in = null;
    OutputStream out = null;
    File file = new File(getFilesDir(), pdfname);
    try
    {
        in = assetManager.open(pdfname);
        out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e)
    {
        Toast.makeText(getApplicationContext(), "Pdf Viewer not installed", Toast.LENGTH_SHORT).show();
    }
    try
    {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(
            Uri.parse("file://" + getFilesDir() + "/"+pdfname),
            "application/pdf");

    startActivity(intent);
    }catch (Exception e) {
        // TODO: handle exception
        Toast.makeText(getApplicationContext(), "Pdf Viewer not installed" ,Toast.LENGTH_SHORT).show();
    }
}

private void copyFile(InputStream in, OutputStream out) throws IOException
{
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1)
    {
        out.write(buffer, 0, read);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接