简体   繁体   English

如何从应用程序打开 Pdf 到默认查看器?

[英]How to open Pdf from application to default viewer?

I'm trying to open pdf from my application to default document viewer in Nougat.我正在尝试将 pdf 从我的应用程序打开到 Nougat 中的默认文档查看器。

This is not duplicate cause I have different problem than others so read till end .这不是重复的,因为我的问题与其他人不同,所以请阅读到最后。

What I've tried:我试过的:

I know i have to use FileProvider , Make provider.xml , get runtime permission about accessing that app .我知道我必须使用 FileProvider,Make provider.xml,获得有关访问该应用程序的运行时权限。

in AndroidManifest.xmlAndroidManifest.xml 中

 <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

my GenericFileProvider我的通用文件提供者

public class GenericFileProvider extends FileProvider {}

provider_path.xml provider_path.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
       <external-path name="external_files" path="."/>
</paths>

openign pdf by this用这个打开 pdf

 val file = File(Environment.getExternalStorageDirectory().toString()+"pdfname")

            val intent = Intent()
            intent.action = Intent.ACTION_VIEW
            val uri = FileProvider.getUriForFile(context,  BuildConfig.APPLICATION_ID + ".provider", file)

           context.grantUriPermission("com.google.android.apps.docs", uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION)


            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

            intent.setDataAndType(uri, "application/pdf")
            context.startActivity(intent)

My problem is :我的问题是:

at the time of viewing my pdf its first character has been changed .在查看我的 pdf 时,它的第一个字符已更改。 so it generates file not found exception所以它生成文件未找到异常

when i'm using this provider path .当我使用此提供程序路径时。 it delete my first character of pdf name它删除了我的 pdf 名称的第一个字符

 my file name is  : /storage/emulated/0on the matter of.pdf
 my uri is : content://mypackage.provider/root/storage/emulated/0on%20the%20matter%20of.pdf
so pdf name is : n the matter of.pdf

when using provider_path this .使用 provider_path 这个时。

<root-path
           name="root"
           path="/" />

it adds 0 to my pdf name它将 0 添加到我的 pdf 名称

 my file name is  : /storage/emulated/0on the matter of.pdf
 my uri is : content://mypackage.provider/root/storage/emulated/0on%20the%20matter%20of.pdf
so pdf name is : 0on the matter of.pdf

What should i do to get pdf name properly.我该怎么做才能正确获取 pdf 名称。 what should i have to write in provider path .我应该在提供程序路径中写什么。

Thanks to @VladyslavMatviienko and @greenapps .感谢 @VladyslavMatviienko 和 @greenapps 。 I've solved my issue.我已经解决了我的问题。

As pointed out by @VladyslavMatviienko .正如@VladyslavMatviienko 指出的那样。 I was passing wrong file path .我传递了错误的文件路径。

val file = File(Environment.getExternalStorageDirectory().toString()+"pdfname")

Instead of代替

val file = File(Environment.getExternalStorageDirectory().toString()+"/pdfname")

So its showing that java.io.FileNotFoundException:所以它显示java.io.FileNotFoundException:

So everything posted in my question is working fine for displaying pdf from my app to viewer in Oreo.因此,在我的问题中发布的所有内容都可以正常工作,以将 pdf 从我的应用程序显示到 Oreo 中的查看器。 So maybe it will help someone.所以也许它会帮助某人。

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

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