将下载的文件保存在公共下载目录中并打开它

时间:2018-01-26 12:55:14

标签: java android android-intent android-download-manager

我无法下载和打开文件。

我在设备的公共外部下载文件夹中成功下载了该文件,我可以通过文件资源管理器打开它,但是当我想从我的应用程序完成下载后立即打开它时, java发生.lang.IllegalArgumentException

 Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /file:/storage/emulated/0/Download/Colors.pdf
            at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:719)
            at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:404)
            at com.example.alla.bitchface.MainActivity$1.onReceive(MainActivity.java:97)
            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1122)
            at android.os.Handler.handleCallback(Handler.java:751) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:154) 
            at android.app.ActivityThread.main(ActivityThread.java:6121) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 

MainActivity.java:97

Uri uri = FileProvider.getUriForFile(MainActivity.this,
                                    BuildConfig.APPLICATION_ID+".provider",
                                    new File(filePath));

我尝试实现FileProvider,但我无法弄清楚如何使其与公共目录一起使用。当我做" hack"删除StrictMode后,文件将打开。这是我的代码:

@Override
    public void onDownloadPrepared(Request downloadRequest, String fileName) {
        downloadRequest.allowScanningByMediaScanner();
        downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                fileName);

        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        if (manager == null) return;

        long id = manager.enqueue(downloadRequest);

        BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                long receivedId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,
                        -1);
                if (id == receivedId) {
                    Query query = new Query();
                    query.setFilterById(id);
                    Cursor cursor = manager.query(query);

                    if (cursor.moveToFirst()) {
                        int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);

                        if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(columnIndex)) {

                            String filePath = cursor.getString(
                                    cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

                            Intent intent1 = new Intent(Intent.ACTION_VIEW);

                            Uri uri = FileProvider.getUriForFile(MainActivity.this,
                                    BuildConfig.APPLICATION_ID+".provider",
                                    new File(filePath));

                            intent1.setData(uri);
                            intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            startActivity(intent1);

                        }
                    }
                    unregisterReceiver(this);
                }
            }
        };

        IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        registerReceiver(downloadReceiver, filter);
    }
<paths>
    <external-path name="external_files" path="."/>
</paths>
<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>

2 个答案:

答案 0 :(得分:0)

删除&#34;文件&#34;路径中的前缀:

filePath = filePath.replace("file:/","");
  

将其发布为Community Wiki Answer,而不是将其编辑到问题中。

答案 1 :(得分:-1)

Download文件夹未列为FileProvider支持的路径。

尝试将以下内容添加到provider_path.xml: