【问题标题】:Opening a downloaded PDF file打开下载的 PDF 文件
【发布时间】:2018-05-25 18:48:52
【问题描述】:

我正在尝试使用 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 的已配置根目录

有什么建议吗?

【问题讨论】:

标签: android android-intent android-fileprovider


【解决方案1】:

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

评估为不可能的文件系统路径。如果您检查了sharedFile.getAbsolutePath() 的值,您会看到它。

改为:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 2011-01-01
    • 2016-11-01
    • 1970-01-01
    相关资源
    最近更新 更多