在牛轧糖上打开PDF [英] Open PDF on Nougat

查看:83
本文介绍了在牛轧糖上打开PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件路径:file:///storage/emulated/0/Android/data/rocks.v d .*****.develop/files/1Q 2017财务结果.pdf

I have a file path: file:///storage/emulated/0/Android/data/rocks.vd.*****.develop/files/1Q 2017 financial results.pdf

这是代码:

Intent intent = new Intent(Intent.ACTION_VIEW);
    try {
        String newFilePath = filePath.replaceAll("%20", " ");
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            intent.setDataAndType(Uri.parse(newFilePath), "application/pdf");
        } else {
            Uri uri = Uri.parse(newFilePath);
            File file = new File(uri.getPath());
            if (file.exists()){
                uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
                intent.setDataAndType(uri, "application/pdf");
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
        }

        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        context.startActivity(intent);

我的uri = content://rocks.v d .*****.develop.provider/files/Android/data/rocks.v d . *****.develop/files/1Q%202017%20financial%20results.pdf

My uri = content://rocks.vd.*****.develop.provider/files/Android/data/rocks.vd.*****.develop/files/1Q%202017%20financial%20results.pdf

Pdf阅读器正在打开,出现意外错误. 怎么了?

Pdf reader is opening with Unexpected error. Whats wrong?

推荐答案

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

在这里您要清除所有现有标志,尤其是您的FLAG_GRANT_READ_URI_PERMISSION.结果,您启动的活动将无法访问您的内容.

Here you are wiping out all your existing flags, notably your FLAG_GRANT_READ_URI_PERMISSION. As a result, the activity that you start will not have access to your content.

setFlags()切换到addFlags().

这篇关于在牛轧糖上打开PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆