【问题标题】:Show PDF in Android在 Android 中显示 PDF
【发布时间】:2010-09-14 05:45:21
【问题描述】:

在我的 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 上,这不会返回匹配项,即使我有 Adob​​e 的 PDF 查看器。这个问题的答案android: open a pdf from my app using the built in pdf viewer 提到 Adob​​e 可能没有任何公共 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);

【问题讨论】:

    标签: android pdf


    【解决方案1】:

    将您的手机连接到您的 PC,启动 Eclipse 并打开 LogCat。然后用浏览器下载PDF文件并打开。你应该看到这样一行(我用了 HTC 的愿望):

    09-14 17:45:58.152:INFO/ActivityManager(79):开始活动: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 阅读器。但是您可以先尝试隐式意图,如果失败,请尝试显式意图作为后备。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2015-11-22
        • 2015-11-04
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 2012-04-11
        • 1970-01-01
        • 2019-02-08
        • 1970-01-01
        相关资源
        最近更新 更多