android - 无法在 Nougat 中使用 Adob​​e 打开 PDF 文件

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

我进行了很多研究,但仍然没有找到在 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;
}

list .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 时显示 toast 消息;

File could not be access please check location or network connection.

最佳答案

终于知道错在哪里了。从 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");            
    }

关于android - 无法在 Nougat 中使用 Adob​​e 打开 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46074702/

相关文章:

安卓 Facebook 分享不工作

android - @BidingMethod 不适用于方法

Android: float 按钮图标未居中

c# - 从浏览器打开关联文档(PDF、DOCS 等)

actionscript-3 - 动态选择静态嵌入图像?

android - 如何在Android中设计EditText和Button

pdf - 如何在某个点以编程方式打开 pdf?

java - 打印错误

php - rtmp是保护图片的唯一方法吗

flash - as3 : youtube api/possible to add youtube's "like-button" (rating)?