【问题标题】:Adobe Reader won't open some valid pdfs from Intent and content:/// on AndroidAdobe Reader 不会在 Android 上从 Intent 和 content:/// 打开一些有效的 pdf
【发布时间】:2017-04-12 19:12:27
【问题描述】:

我正在尝试实现一种方法来打开位于 /Android/data/package.name/files/ 中的某些文件的 PDF 阅读器。我未能使用适当的 FileProvider,但我发现刚刚添加 content:// 就可以了。但是由于某种原因,其中一些无法打开,我不明白为什么。

File file=new File("Direct path to file as string");

if(file.exists()){  ///Yes, File Exists
            try {

                Intent intent = new Intent(Intent.ACTION_VIEW);
                Log.v("Authority", context.getApplicationContext().getPackageName());
                Log.v("URI", Util.UriForFile(context, file).toString());
                intent.setDataAndType(Util.UriForFile(context, file), "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                context.startActivity(intent);


            } catch (ActivityNotFoundException e) {
                parent.alert(context.getString(R.string.error_nopdfreader));
            }
}

和实际方法Util.UriForFile:

public static Uri UriForFile(Context context, File file){
    return Uri.parse("content://"+file.getAbsolutePath());
//        if(context==null)
//            Log.v("Util", "Context is null");
//
//        if(file==null)
//            Log.v("Util", "File is null");
//
//        if(context.getApplicationInfo()==null)
//            Log.v("Util", "getApplicationInfo is null");
//
 //        return FileProvider.getUriForFile(context,  context.getApplicationInfo().packageName+".provider", file);
}

【问题讨论】:

  • “我发现刚刚添加的内容:// 完成了这项工作”——这将适用于大约零个应用程序。如果它与 Adob​​e Reader 一起使用,那是 Adob​​e Reader 中的一个错误。那是一个无效的Uri(例如,它没有权限字符串)。 “我在使用正确的 FileProvider 时失败了”——考虑提出一个单独的 Stack Overflow 问题,您可以在其中提供 minimal reproducible example 并详细解释“失败”的含义。
  • 好吧,这里描述了我的 FileProvider 问题的一半:stackoverflow.com/questions/40318116/…。除此之外,我还想让用户在 Android/data 文件夹之外指定另一个目录,甚至在辅助存储上,但使用 content:/// 是唯一有效的方法
  • 即使在内部存储上使用 FileProvider,Adobe 也不会打开某些特定文件。似乎无法打开的文件是那些在实际文件名中带有“%20”的文件(没有空格,但真正的“%20”字符被 FileProvider 编码为“%2520”)。

标签: android pdf android-intent uri android-fileprovider


【解决方案1】:

好的,我“神奇地”通过返回使用 FileProvider(取消注释行)解决了这个问题,并进行了 2 处修改:

  1. 在意图的标志中添加 FLAG_GRANT_READ_URI_PERMISSION
  2. 添加“未记录的”文件路径(用于访问 Android/data 之外的文件):

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

【讨论】:

  • 我遇到了类似的问题。除 Adob​​e Reader 外,所有 PDF 应用程序都可以通过 Intent 打开文件。我的问题是文件名。最后没有.pdf。不知道为什么这对其他应用来说不是问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多