打开下载的 PDF 文件

Opening a downloaded PDF file

我正在尝试使用 FileProvider 通过隐式意图打开下载的 pdf 文件。

我正在使用 DownloadManager 从远程服务器下载 pdf 文件,它工作正常。哪个存储在它的目的地。

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadURL));
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    request.setAllowedOverRoaming(false);
    request.setTitle(mFilename);
    request.setDescription("Downloading...");
    request.setVisibleInDownloadsUi(true);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/FOLDER_NAME/" + mFilename);

下载完成后我想打开它。

public void OpenPdfFile(){
    File sharedFile = new File(Environment.DIRECTORY_DOWNLOADS, "/FOLDER_NAME/" + mFilename);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID+ ".provider", sharedFile);
    intent.setDataAndType(uri, "application/pdf");

    PackageManager pm = mContext.getPackageManager();
    if (intent.resolveActivity(pm) != null) {
        mContext.startActivity(intent);
    }
}

在清单文件中

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        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 xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external-path" path="." />
</paths>

我遇到了这个错误

java.lang.IllegalArgumentException:找不到包含 /Download/FOLDER_NAME/demo_presentationfile.PDF

的已配置根目录

有什么建议吗?

File sharedFile = new File(Environment.DIRECTORY_DOWNLOADS, "/FOLDER_NAME/" + mFilename);

评估为不可能的文件系统路径。如果您检查 sharedFile.getAbsolutePath().

的值,您会看到

更改为:

File sharedFile = new File(Environment.getExternalStoragePublicDirectory( 
        Environment.DIRECTORY_DOWNLOADS), "FOLDER_NAME/" + mFilename);