【问题标题】:Not able to Open PDF file by using Adobe in Nougat在 Nougat 中使用 Adob​​e 无法打开 PDF 文件
【发布时间】:2017-09-06 12:01:11
【问题描述】:

我进行了很多研究,但仍然没有找到在 Adob​​e 阅读器中打开下载的 pdf 的正确解决方案。 通过使用下面的代码,pdf 可以使用 google doc 应用程序打开,而不是 adobe。

从 Url 下载 pdf

 URL url = new URL(f_url[0]);
        try {
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.connect();
                    InputStream inputStream = urlConnection.getInputStream();
                    FileOutputStream fileOutputStream = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + getFileName());

                    byte[] buffer = new byte[AccessibilityNodeInfoCompat.ACTION_DISMISS];
                    while (true) {
                        int bufferLength = inputStream.read(buffer);
                        if (bufferLength <= 0) {
                            break;
                        }
                        fileOutputStream.write(buffer, 0, bufferLength);
                    }
                    fileOutputStream.close();
                } catch (Exception e) {
                    FileLog.e("PDF Save", e);
                }

查看保存的 PDF

    File file = new File(Environment.getExternalStorageDirectory() + "/" + getFileName());
    try {
           if (file.exists()) {
               Uri fileUri = Uri.fromFile(file);
            if (Utilities.isAboveNougat()) {
            Uri contentUri = FileProvider.getUriForFile(mContext, "myPackage.fileprovider", file);
             Intent intent = new Intent();
             intent.setAction(Intent.ACTION_VIEW);
             intent.setDataAndType(contentUri, "application/pdf");         
             intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
             intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            mContext.startActivity(intent);

         } else {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(fileUri, "application/pdf");
             mContext.startActivity(intent);
         }

    } else {
     Toast.makeText(mContext, "Not able to find downloaded file", Toast.LENGTH_LONG).show();
     }

    } catch (ActivityNotFoundException ae) {
       Toast.makeText(mContext, "No activity found to open this attachment.", Toast.LENGTH_LONG).show();
    }
public static boolean isAboveNougat() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return true;
    }
    return false;
}

Manifest.xml;

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

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path path="." name="external_storage_root" />
</paths>

我也尝试了以下方式打开pdf;

Intent intent = ShareCompat.IntentBuilder.from((Activity) mContext)
                                .setStream(contentUri)
                                .setType("application/pdf")
                                .getIntent()
                                .setAction(Intent.ACTION_VIEW)
                                .setDataAndType(contentUri, "application/pdf")                                .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(intent);

但仍然无法解决问题。使用 adobe 打开 pdf 时显示吐司消息;

文件无法访问,请检查位置或网络连接。

【问题讨论】:

  • 能否在 manifest、provider.xml 和 Utilities.isAboveNougat() 代码中发布 fileprovider 元数据
  • 你能发布你的清单吗?
  • 我已经编辑了我的问题。
  • @priyankakamthe 只是为了测试一次,您可以将 pdf 文件保存在特定文件夹中,例如 pdfs 并将该文件夹名称放在 试试?
  • @Raghavendra 我试过但没用 :(

标签: android pdf adobe android-7.0-nougat android-fileprovider


【解决方案1】:

我终于知道我在哪里犯了错误。从 Url 下载 pdf 时,它会存储在字节中,而 adobe 无法读取该字节格式的文件。因此,在保存下载的文件时,我们需要提供 .pdf 扩展名,而我在存储它时没有提供。大多数文档查看应用程序都能够读取字节格式的文件,如谷歌的文档应用程序。

我对 getFileName 函数进行了更改;

private String getFileName() {
        String tempFileName = urlString.substring(urlString.length() - 10);
        String[] arrFilenames = tempFileName.split("\\.");
        return Utilities.getString(arrFilenames[0]+".pdf");            
    }

【讨论】:

    【解决方案2】:
    Uri contentUri = FileProvider.getUriForFile(mContext, "myPackage.fileprovider", file);
             Intent intent = new Intent();
             intent.setAction(Intent.ACTION_VIEW);
             intent.setDataAndType(contentUri, "application/pdf");         
             intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
             intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            mContext.startActivity(intent);
    

    牛轧糖这对我有用

    【讨论】:

    • 注意:“myPackage.fileprovider”这应该和我们在 FileProvider 文件中提到的一样。
    猜你喜欢
    • 2019-02-16
    • 1970-01-01
    • 2011-06-23
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2013-11-29
    相关资源
    最近更新 更多