Android中打开pdf文件的限制

Android中打开pdf文件的限制,android,pdf,Android,Pdf,我正在尝试从我的Android应用程序中打开一些pdf。我使用的是这样做的意图: Intent intent = new Intent(); intent.setDataAndType(Uri.parse(url), "application/pdf"); startActivity(intent); 这段代码适用于某些pdf,但当我尝试打开其他pdf时失败 这是Android向我展示的信息: 文件有问题 我必须提到,打开时没有问题的pdf是用一个Crystal Report模板创建的,而失败

我正在尝试从我的Android应用程序中打开一些pdf。我使用的是这样做的意图:

Intent intent = new Intent();
intent.setDataAndType(Uri.parse(url), "application/pdf");
startActivity(intent);
这段代码适用于某些pdf,但当我尝试打开其他pdf时失败

这是Android向我展示的信息:

文件有问题

我必须提到,打开时没有问题的pdf是用一个Crystal Report模板创建的,而失败的pdf是用另一个模板创建的

相反,如果我打开浏览器(在我的计算机上)上出现故障的pdf的url,打开它们不会给我任何错误,因此我猜Android上可能有一些限制,不同于其他pdf(在Crystal Report模板上),但我看不到它

在Android上打开pdf文件存在哪些限制?(尺寸、晶体报告中不允许的某些参数等)

我已经放弃了这可能是一个大小限制,因为给出问题的pdf文件比没有给出任何错误的文件小

证明我已经做到了:

  • 在浏览器上打开错误的PDF~~>OK
  • 在手机上下载错误的PDF并打开它~~~>OK
  • 在应用程序上打开错误的PDF ~~>错误
  • 在PDF崩溃的公司的应用程序上打开良好的PDF ~~>OK
编辑

我注意到我使用的是
http://
协议,但是PDF是在
https://
协议上的,所以我在
Uri.parse
方法上对其进行了更改

当我进行此更改时,应用程序崩溃,日志上显示错误:

android.content.ActivityNotFoundException:未找到可处理意图的活动

另外,我注意到,没有给我任何错误的PDF位于带有
http://
协议的url中,而不是
https://
,因此我猜
https://
协议可能是问题所在


我只能打开http://请求吗?

可能是Android PDF viewer应用程序无法解释该文件。 您是否尝试过将完全相同的文件复制/下载到您的Android手机并从那里打开

此外,我建议使用IntentChooser启动PDF viewer,只是为了在未安装PDF viewer的情况下保持安全/让用户选择应用程序:

Intent intent = new Intent();
intent.setDataAndType(Uri.parse(url), "application/pdf");
Intent chooserIntent = Intent.createChooser(intent, "Open Report");
startActivity(chooserIntent);

使用意图打开带有
https://
协议的pdf文件,显然
https://
不是问题所在

我看到您正在尝试这种定义数据类型的方法:

Intent intent = new Intent();
intent.setDataAndType(Uri.parse(url), "application/pdf");
startActivity(intent);
但这将导致:

ActivityNotFoundException:未找到可处理意图的活动

如果使用其他方法,则在打开此类文件的选项中可能看不到PDF阅读器:

  Intent intent = new Intent();
  intent.setDataAndType(Uri.parse(url), "application/pdf");
  Intent chooserIntent = Intent.createChooser(intent, "Open Report");
  startActivity(chooserIntent);
您必须在不使用类型定义的情况下尝试此方法,它将非常有效:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);

通过intent打开pdf文件时出现此问题的其他原因是打开此类文件的默认应用程序,因此您可能配置了损坏或无效的应用程序,因此请重置默认值:

转到
设置
。 轻触应用程序。 点击默认应用程序


选择应用程序并清除默认值

我找到了一个在Android应用程序上查看PDF的解决方法(但不允许我在应用程序上显示后下载)。如果我用谷歌文档打开我的PDF,我可以打开我的意图

这是我在url之前必须添加的内容:

https://docs.google.com/gview?embedded=true&url=
因此,最终的url如下所示:

https://docs.google.com/gview?embedded=true&url=https://myUrl.pdf
下面是我现在使用的全部代码:

String url= "https://docs.google.com/gview?embedded=true&url=https://url.pdf";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
但它仍然不够,因为我想在不需要使用第三方应用程序的情况下打开它。另外,用谷歌文档打开PDF的速度很慢,我不得不等太久,直到PDF最终打开

如果有人知道如何用原生Android打开它,请告诉我


如果我不使用谷歌文档打开它会发生什么?

使用相同的代码,但只是使用我的url,而不是添加的Google Docs url,Android让我在两个应用程序之间进行选择:Adobe Acrobat Reader和Google Chrome

  • 如果我用谷歌浏览器打开它,它会下载PDF,但不会自动打开

  • 如果我用Adobe Acrobat Reader打开它,它会给我以下错误:

    无法显示PDF(无法打开)

    • 你可以试试这个

      public void OpenPDFFile(String file_name) {
          try {
              File pdfFile = new File(getFile(), file_name);//File path
              if (pdfFile.exists()) //Checking for the file is exist or not
              {
                  Uri path = Uri.fromFile(pdfFile);
                  Intent objIntent = new Intent(Intent.ACTION_VIEW);
                  objIntent.setDataAndType(path, "application/pdf");
                  objIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                  try {
                      startActivity(objIntent);
                      Log.e("IR", "No exception");
                  }
                  catch (ActivityNotFoundException e) {
                      Log.e("IR", "error: " + e.getMessage());
                      Toast.makeText(DownloadedPdfActivity.this,
                              "No Application Available to View PDF",
                              Toast.LENGTH_SHORT).show();
                  }
      
              }
          }catch (Exception e){
              e.printStackTrace();
          }
      }
      


      您可以在webview中显示pdf

      如果API>=21,您可以使用PDFRender创建每个页面的位图,但它只能查看,不能编辑。下面是一个我在飞行中制作的示例,缺少导航按钮,但这些按钮应该不难实现

      PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(new File("/path/to/file.pdf"), 
                  ParcelFileDescriptor.MODE_READ_ONLY));
          PdfRenderer.Page page = renderer.openPage(0);
          Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),
                  Bitmap.Config.ARGB_8888);
          page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
          imageView.setImageBitmap(bitmap);
          page.close();
          renderer.close();
      
      编辑


      PdfRenderer需要一个本地文件作为FileDescriptor。因此,据我所知,通过“云”进行查看是不可能的。

      简单地为android添加以下库:

      //in app level build
      compile 'com.joanzapata.pdfview:android-pdfview:1.0.4@aar'
      
      //inside your xml file
      <com.joanzapata.pdfview.PDFView
          android:id="@+id/pdfview"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>
      
      //inside java code
       pdfView.fromAsset(pdfName)
      .pages(0, 2, 1, 3, 3, 3)
      .defaultPage(1)
      .showMinimap(false)
      .enableSwipe(true)
      .onDraw(onDrawListener)
      .onLoad(onLoadCompleteListener)
      .onPageChange(onPageChangeListener)
      .load();
      
      //应用程序级内部构建
      编译'com.joannazapata.pdfview:android pdfview:1.0。4@aar'
      //在xml文件中
      //内部java代码
      pdfView.fromAsset(pdfName)
      .页(0、2、1、3、3、3)
      。第(1)页
      .showMinimap(错误)
      .enableSwipe(真)
      .onDraw(onDrawListener)
      .onLoad(onLoadCompleteListener)
      .onPageChange(onPageChangeListener)
      .load();
      
      有关更多信息,请使用此GitHub链接:

      您可以使用Webview从url打开PDF,如下所示:

      webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + your url);
      

      您可以为android使用以下库:-//应用程序级编译

      编译'com.github.barteksc:android pdf查看器:2.6.1'

      在xml中:

      <com.github.barteksc.pdfviewer.PDFView
              android:id="@+id/pdfView"
              android:layout_width="match_parent"
              android:layout_height="match_parent"/>
      

      欲了解更多信息,请参阅此

      您的问题是您的pdf正在数据/数据文件夹内下载,当您尝试使用默认意图打开时,外部应用程序没有从数据/数据文件夹内打开文件的任何权限,因此您需要在应用程序的目录外下载文件,然后尝试重新打开它正在工作。

      @matrix我尝试过使用Andro
      <com.github.barteksc.pdfviewer.PDFView
              android:id="@+id/pdfView"
              android:layout_width="match_parent"
              android:layout_height="match_parent"/>
      
      pdfView.fromUri(Uri)
      or
      pdfView.fromFile(File)
      or
      pdfView.fromBytes(byte[])
      or
      pdfView.fromStream(InputStream) // stream is written to bytearray - native code cannot use Java Streams
      or
      pdfView.fromSource(DocumentSource)
      or
      pdfView.fromAsset(String)