简体   繁体   English

在 Android 10 上阅读 PDF

[英]Read a PDF on Android 10

Need to read a PDF on Android 10 The following code works fine on Android 9.0 but on Android 10 the application responsable for show the PDF just blink and return to my application.需要在 Android 10 上阅读 PDF 以下代码在 Android 9.0 上运行良好,但在 Android 10 上,负责显示 PDF 的应用程序只是闪烁并返回到我的应用程序。

            File pdfFile = new File(getExternalFilesDir(null), "Pdf");
            pdfFile.setReadable(true);
            if (pdfFile.exists()) {
                if (pdfFile.canRead()) {
                    if (pdfFile.canWrite()) {
                        Uri path = Uri.fromFile(pdfFile);
                        Intent objIntent = new Intent(Intent.ACTION_VIEW);
                        objIntent.setDataAndType(path, "application/pdf");
                        objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                        startActivityForResult(objIntent, req_code);
                    }else{
                        Toast.makeText(MainActivity.this, "Not writable! ", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(MainActivity.this, "Not readable! ", Toast.LENGTH_SHORT).show();
                }
            }else{
                Toast.makeText(MainActivity.this, "The file does not exists! ", Toast.LENGTH_SHORT).show();
            }

On the method onActivityResult I'm receiving RESULT_CANCELED as result_code and data == null.在 onActivityResult 方法上,我收到 RESULT_CANCELED 作为 result_code 和 data == null。

The following code works fine on Android 9.0以下代码在 Android 9.0 上运行良好

Somebody on your project did something unfortunate to allow that code to work on Android 9. It should have failed three years ago, starting with Android 7.0, with a FileUriExposedException .您项目中的某个人做了一些不幸的事情,让该代码在 Android 9 上运行。它应该在三年前失败,从 Android 7.0 开始,带有FileUriExposedException

On Android 10, apps do not have access to external storage, except for a few limited app-specific locations.在 Android 10 上,应用无权访问外部存储,除了少数特定于应用的有限位置。 This is what Google was warning about starting with Android 7.0, that you should not use Uri.fromFile() anymore.这就是谷歌从 Android 7.0 开始警告你不应该再使用Uri.fromFile()警告。

The right solution three years ago — and the right solution today — is to use FileProvider to make that PDF available to the PDF viewer app.三年前的正确解决方案——今天也是正确的解决方案——是使用FileProvider使 PDF 可用于 PDF 查看器应用程序。

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM