android - 为手机上的标准下载目录创建文件提供程序,以通过 Intent 发送 PDF 文件

标签 android pdf android-intent android-fileprovider

当我将它传递给 PDF 查看器时 - 它出错并声明它无法打开该文件。 GoogleViewer 在日志中打印以下内容

2019-01-24 15:36:55.390 23756-23833/? V/LoadDocumentTask: Finish task: LoadDocumentTask(Display Data [PDF : mypdf.pdf] +ContentOpenable, uri: content://com.example.gregm.pdfplaygorund.provider/Download/Download/mypdf.pdf) result=FILE_ERROR time=16ms

发送 Intent 的代码

    String filepath = "storage/emulated/0/Download/mypdf.pdf";
            File file = new File(filepath);
            if(file.exists())
            {
                Log.e("ERRR", "file exists");
            }

            Intent intent = new Intent(Intent.ACTION_VIEW);
            Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",file);

         intent.setDataAndType(uri, "application/pdf");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |Intent.FLAG_ACTIVITY_NEW_TASK);

            List<ResolveInfo> resInfoList = MainActivity.this.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
            for (ResolveInfo resolveInfo : resInfoList) {
                String packageName = resolveInfo.activityInfo.packageName;
                MainActivity.this.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }

            startActivity(intent); // Crashes on this line

res/xml/provider_paths.xml

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

安卓 list .xml

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.gregm.pdfplaygorund.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

最佳答案

在 list 中包含这些逻辑是不够的

我还必须请求许可

ActivityCompat.requestPermissions(ma,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                            MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
    ActivityCompat.requestPermissions(ma,
                                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                    MY_PERMISSIONS_READ_EXTERNAL_STORAGE);

然后上面的代码就可以工作了

关于android - 为手机上的标准下载目录创建文件提供程序,以通过 Intent 发送 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54355031/

相关文章:

android - 录制视频时禁用方向更改

c# - ITextSharp 将文本插入现有的 pdf

javascript - 使用 JSF 和 Viewer.js 从文件系统打开 PDF 文件

Android:没有用户初始化的来电号码?

android - 过滤 ACTION_SEND 共享选项

android - 如何根据 Kotlin 中的进度条进度更新 textview 值

java - 通过 cellId、LAC、MCC 和 MNC Android 获取当前移动站与相邻基站的距离和方向?

android - 如何在 Kotlin Native 中使用 kotlin.system?

php - 命令行后脚本不继续

android - 如何从我的 Android 应用程序打开 Linkedin 应用程序?