Android 以编程方式读取pdf文件会产生错误

Android 以编程方式读取pdf文件会产生错误,android,Android,现在我正在做一个Android应用程序。我必须从SD卡读取一个文件。我在模拟器中安装了adobe reader。我使用以下代码从SD卡运行该文件 intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("/sdcard/samp.pdf"), "application/pdf"); try{ startActivity(intent); } catch (A

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

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”)

现在它开始工作了。但是现在我将sample.pdf文件夹放在我的assets文件夹中,并编写以下代码

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

我收到的文件路径无效消息…

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

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

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


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

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

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

您有:

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

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

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

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

我插入了samp和samplei插入了samp和samplei进行了一些更改,现在它工作了。。。我删除content.setDataAndType(Uri.parse(“/sdcard/samp.pdf”),“application/pdf”);并添加File=new文件(“/sdcard/sample.pdf”);Uri=Uri.fromFile(文件);setDataAndType(uri,“应用程序/pdf”);嗨..现在它对sdcard有效,但对我放在assets文件夹中的文件无效。如何从assets文件夹中读取相同的文件。我给出了file file=new file(“/sdcard/sample.pdf”);Uri=Uri.fromFile(文件);setDataAndType(uri,“应用程序/pdf”);但是没有找到文件路径。我把我的文件放在资产文件夹中,然后写file file=new file(“安卓”)。resource://com.pdftest/assets/sample");..但是它的给出路径没有找到…看这个问题嗨…我读了答案,但我不知道如何生成代码…你能给我示例代码吗…?我做了一些更改,现在它工作了。。。我删除content.setDataAndType(Uri.parse(“/sdcard/samp.pdf”),“application/pdf”);并添加File=new文件(“/sdcard/sample.pdf”);Uri=Uri.fromFile(文件);setDataAndType(uri,“应用程序/pdf”);嗨..现在它对sdcard有效,但对我放在assets文件夹中的文件无效。如何从assets文件夹中读取相同的文件。我给出了file file=new file(“/sdcard/sample.pdf”);Uri=Uri.fromFile(文件);setDataAndType(uri,“应用程序/pdf”);但是没有找到文件路径。我把我的文件放在资产文件夹中,然后写file file=new file(“安卓”)。resource://com.pdftest/assets/sample");..但它的给出路径没有找到…看这个问题嗨…我读了答案,但我不知道如何制作代码…你能给我一个示例代码吗。。。?