首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >打算在Android中打开PDF文件

打算在Android中打开PDF文件
EN

Stack Overflow用户
提问于 2017-03-28 10:34:14
回答 2查看 5.2K关注 0票数 2

我一直试图打开一个PDF文件使用的意图。它适用于Adroid之前的设备。下面是我使用的代码

代码语言:javascript
运行
复制
File file = new File(gridItems.get(position).getPath());
                        Intent intent = null;
                        if (Build.VERSION.SDK_INT < 24) {
                            intent = new Intent(Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.fromFile(file), "application/pdf");
                            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                        } else {
                            intent = new Intent();
                            intent.setAction(Intent.ACTION_VIEW);
                            Uri pdfURI = FileProvider.getUriForFile(GalleryPdfActivity.this, getApplicationContext()
                                    .getPackageName
                                            () +
                                    ".provider", file);
                            intent.putExtra(Intent.EXTRA_STREAM, pdfURI);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                            intent.setType("application/pdf");
                        }
                        try {
                            if (intent.resolveActivity(getPackageManager()) != null)
                                startActivity(intent);
                            else
                                AppUtils.toast("No Application found to open the pdf", GalleryPdfActivity.this);
                        } catch (Exception e) {
                            AppUtils.toast(e.getMessage(), GalleryPdfActivity.this);
                        }

文件选择器打开,我已经选择谷歌PDF查看器打开应用程序。但是它返回一个错误“无法显示PDF(没有收到文件)”。在Android之前,我能够在设备中打开相同的文件

EN

Stack Overflow用户

发布于 2018-06-07 08:16:46

添加FLAG_GRANT_READ_URI_PERMISSION的

代码语言:javascript
运行
复制
Intent intent = new Intent(Intent.ACTION_VIEW)
Uri outputFileUri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", file); 
intent.setDataAndType(outputFileUri, "application/pdf"); 
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
Intent in = Intent.createChooser(intent, "Open File");
startActivity(in);

还需要在res provider_paths.xml文件夹中添加->,并需要在清单中添加以下代码

代码语言:javascript
运行
复制
<application>
   <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true"
            tools:replace="android:authorities">
            <meta-data
                tools:replace="android:resource"
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>
 </application>
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43067238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档