PDF文件路径不正确

时间:2015-06-15 07:24:53

标签: java android pdf android-studio filepath

我想在我的应用程序中打开一个PDF文件但每次都出错了。 是的,我看了很多关于它的话题,但是没有一个能帮助我。

以下是错误的一些照片:

http://i.stack.imgur.com/TZpO2.jpg

http://i.stack.imgur.com/CuoeM.jpg

 btn3.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    //startActivity(new Intent(MainActivity.this, Activity2.class));
                                    File file = null;
                                    file = new File(Environment.getExternalStorageDirectory() + "/raw/" + "tirepressuremonitoringsystem3.pdf");
                                    Toast.makeText(getApplicationContext(), file.toString() , Toast.LENGTH_LONG).show();
                                    if(file.exists()) {
                                        Intent target = new Intent(Intent.ACTION_VIEW);
                                        target.setDataAndType(Uri.fromFile(file), "application/pdf");
                                        target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

                                        Intent intent = Intent.createChooser(target, "Open File");
                                        try {
                                            startActivity(intent);
                                        } catch (ActivityNotFoundException e) {
                                            // Instruct the user to install a PDF reader here, or something
                                        }
                                    }
                                    else
                                        Toast.makeText(getApplicationContext(), "File path is incorrect." , Toast.LENGTH_LONG).show();
                                }
                            }
    );

可能错误是我将这些文件放在 / raw 中?我应该将它们存储在 assest文件夹 中吗?

3 个答案:

答案 0 :(得分:1)

您似乎想要从原始文件夹 @gdd访问文件。
由于在编译您的Android项目时,R.java文件被生成,您可以访问任何内容你的原始文件夹是 R.raw.fileName ,在你的情况下你想要访问文件名tirepressuremonitoringsystem3.pdf,所以你可以使用 R. raw.tirepressuremonitoringsystem3

如果你不想这样做并想使用jdks文件api .. this question will help you 谢谢并继续编码。

答案 1 :(得分:0)

替换你的这一行

 file = new File(Environment.getExternalStorageDirectory() + "/raw/" + "tirepressuremonitoringsystem3.pdf");

这一行

 file = new File("android.resource://com.cpt.sample/raw/tirepressuremonitoringsystem3.pdf");

答案 2 :(得分:0)

如果要在运行时通过标识符访问它,请使用raw:

R.raw.tirepressuremonitoringsystem3

如果您想通过字符串名称访问资产,请使用资产。

无论哪种方式,您都需要将其从应用中复制到其他应用有权访问的位置,详见以下答案: