gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:07:16 26 4
gpt4 key购买 nike

当我将它传递给 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com