【问题标题】:PDF file path is incorrect [duplicate]PDF文件路径不正确[重复]
【发布时间】:2015-08-30 14:41:55
【问题描述】:

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

以下是一些错误照片:

 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 中?我应该将它们存储在 asset 文件夹 中吗?

【问题讨论】:

标签: java android pdf android-studio filepath


【解决方案1】:

您似乎想从 raw 文件夹 @gdd 访问文件。

由于在编译您的 android 项目时会生成 R.java 文件,因此您可以访问原始文件夹中的任何内容R.raw.fileName 文件夹,在您的情况下,您想访问文件名 Tirepressuremonitoringsystem3.pdf,因此您可以使用 R.raw。胎压监测系统3

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

【讨论】:

    【解决方案2】:

    替换你的这一行

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

    这一行

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

    【讨论】:

    • i.imgur.com/jKrwPKh.jpg 现在我明白了
    • 用你的包名替换 com.cpt.sample
    • 如果你不能给出静态包名然后放置这一行 File file = new File("android.resource://+" + getPackageName() +"/raw/tirepressuremonitoringsystem3.pdf") ;
    • 我改成了完整路径:file = new File("android.resource://com.example.dovydas.myapplication3/res/raw/tirepressuremonitoringsystem3.pdf");仍然路径不好
    • com.example.dovydas.myapplication3 这个AndroidManifest.xml文件是包名吗?
    【解决方案3】:

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

    R.raw.tirepressuremonitoringsystem3
    

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

    无论哪种方式,您都需要将其从您的应用程序复制到另一个应用程序可以访问的位置,如以下答案中所述:

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 2015-09-27
    • 2015-07-06
    相关资源
    最近更新 更多