gpt4 book ai didi

android - 驱动器打不开的pdf

转载 作者:行者123 更新时间:2023-12-05 00:04:44 24 4
gpt4 key购买 nike

用例:通过设备中存在的任何pdf阅读器应用程序(例如:GDrive)在android中打开pdf(存在于我的应用程序名称目录的内部存储中)。

     intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
context.startActivity(intent);

问题 根据我目前的分析,它在 android 9 及以下设备中运行良好,但在 Android 10 中产生了问题。

发生了什么: Pdf 名称显示在 Gdrive pdf 查看器标题中,但 pdf 不呈现。并带回我的应用程序。

产生问题的设备:三星s 10(不知道安卓版本)、一加A6000(安卓10)、三星A70(不知道安卓版本)、诺基亚6.1 plus(安卓10)、一加6T(不知道安卓版本)

虽然有些设备在 android 10 上运行并且没有造成问题。

未产生问题的设备:三星 GalaxyS20(Android 10)

也有少数设备在早期使用 android 10 时没有产生问题,并且最近开始产生问题,反之亦然(即问题自动得到修复)。

这个问题非常令人困惑,我无法找到根本原因。

我尝试过的
1.检查日志:没有错误/异常被记录
  • 检查 Intent 的结果代码也没有问题。
  • 权限:应用内:媒体、相机和存储
    权限:启用
    在驱动器中:存储权限:启用

  • 我还阅读了 android 10 的文档。

    To allow other apps to access files stored in this directory within internal storage, use a FileProvider with the FLAG_GRANT_READ_URI_PERMISSION attribute.


         intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), "application/pdf");
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  • pdf 没有问题,因为用其他一些 pdf 阅读器(例如:adobe)打开相同的 pdf,它可以完美地工作。

  • 当前应用
       compileSdkVersion 28
    minSdkVersion 19
    targetSdkVersion 28

    我只是想解决这个问题。我有几个问题:
  • 它实际上是android 10的问题还是我的代码有问题?
  • 使应用程序与 android 10 兼容会解决这个问题吗?
  • 我该如何调试它?

  • 如果我错过了任何案例,请告诉我。提前致谢。

    最佳答案

    虽然它实际上已经通过使用 FileProvider 得到了回答,但我仍然想分享使用 FileProvider 修复驱动器 pdf 阅读器问题的实现。这里是如何,
    1 - 添加xml文件夹并创建path.xml
    add xml folder
    2 - 在 path.xml 中添加以下代码以配置目录 内的文件访问权限
    我在下载目录中设置了pdf文件位置

    <paths>
    <external-path
    name="external"
    path="."/>
    </paths>
    3 - 在 AndroidManifest.xml 中添加元数据
    <application
    ...>
    {...}
    <provider
    android:authorities="com.example.myapplication"
    android:name="androidx.core.content.FileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/provider_paths" />
    </provider>
    </application>
    4 - 使用 Intent 操作读取 pdf 文件
    var uri = FileProvider.getUriForFile(
    this, "com.example.applicationid", File(
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
    .toString() +
    "/" + "yourfile.pdf"
    )
    )
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, "application/pdf")
    var builder = StrictMode.VmPolicy.Builder()
    StrictMode.setVmPolicy(builder.build())
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    startActivity(intent)
    运行应用程序并尝试使用驱动器 pdf 查看器打开它,瞧!希望对大家有帮助!

    关于android - 驱动器打不开的pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62444823/

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