【问题标题】:reading pdf file programatically is giving error以编程方式读取pdf文件会出错
【发布时间】:2012-02-29 09:20:41
【问题描述】:

现在我正在做一个 Android 应用程序。我必须从 sdcard 读取文件。我在模拟器中安装了 adobe reader。我使用以下代码从 sdcard 运行文件。

intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse("/sdcard/samp.pdf"), "application/pdf");
    try{

        startActivity(intent);
    }
    catch (ActivityNotFoundException e) {
        System.out.println("5");
        // No application to view, ask to download one
        System.out.println("hai");
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("No Application Found");
        builder.setMessage("Download one from Android Market?");
        builder.setPositiveButton("Yes, Please",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                        marketIntent
                                .setData(Uri
                                        .parse("market://details?id=com.adobe.reader"));
                        startActivity(marketIntent);
                    }
                });
        builder.setNegativeButton("No, Thanks", null);
        builder.create().show();
    }

我在 sdcard 中插入了一个名为 sample.pdf 的 pdf。但是当我运行我的应用程序时,我得到“无法打开文件”。但我可以从模拟器手动打开pdf。请帮我找出解决方案。

嗨,朋友们,我得到了解决方案。我删除了 intent.setDataAndType(Uri.parse("/sdcard/samp.pdf"), "application/pdf"); 并添加

 File file = new File("/sdcard/sample.pdf");
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "application/pdf");

现在它的工作。但是现在我把sample.pdf文件夹放到我的assets文件夹里,写下如下代码。

 File file = new File("android.resource://com.pdftest/assets/sample");
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "application/pdf"); 

我收到文件路径无效消息...

【问题讨论】:

    标签: android


    【解决方案1】:

    你有:

     File file = new File("android.resource://com.pdftest/assets/sample");
    

    不应该是:

    File file = new File("android.resource://com.pdftest/assets/sample.pdf");
    

    【讨论】:

      【解决方案2】:

      Uri 必须包含的不仅仅是路径。如果您查看 Uri 的内容,它是空的。要获得正确的 Uri,请使用 Uri.fromFile()

      file = new File(Environment.getExternalStorageDirectory()+"/samp.pdf");
      if (file.exists())
      {
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/pdf");
        startActivity(intent);
      }
      else
      {
      Log.e("File not Exist","Check pdf File");
      }
      

      【讨论】:

      • 我做了一些改变,现在它可以工作了...我删除了intent.setDataAndType(Uri.parse("/sdcard/samp.pdf"), "application/pdf");并添加 File file = new File("/sdcard/sample.pdf"); Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, "application/pdf");
      • 嗨..现在它适用于 sdcard 但不适用于我放在 assets 文件夹中的文件。如何从 assets 文件夹中读取相同的文件。我给了 File file = new File("/sdcard/sample .pdf"); Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, "application/pdf");但找不到文件路径
      • ya..我将我的文件放入资产文件夹并写入 File file = new File("android.resource://com.pdftest/assets/sample");..但它的给出路径没找到...
      • 嗨..我读了答案,但我不明白如何制作代码...你能给我示例代码吗...?
      【解决方案3】:

      您是否显示了正确的路径?可能是路径问题。

      【讨论】:

        【解决方案4】:

        只是看看你有: sdcard/samp.pdf 是文件名,你是说你的文件名是:sample.pdf?

        【讨论】:

        • 我插入了样品和样品
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-25
        • 2011-01-25
        • 2013-02-23
        • 2011-11-28
        • 1970-01-01
        • 2011-08-22
        • 2017-04-25
        相关资源
        最近更新 更多