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

Not able to Open PDF file by using Adobe in Nougat

我进行了很多研究,但仍然没有找到在 Adob​​e reader 中打开下载的 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 时显示 toast 消息;

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

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

我更改了 getFileName 函数;

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

牛轧糖适合我