FileProvider - 从下载目录中打开文件

Sar*_*ara 15 directory android download android-download-manager android-fileprovider

我无法从"下载文件夹"中打开任何文件.

我可以下载一个文件并保存在下载文件夹中:

   DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setDescription(descricao);
    request.setTitle(titulo);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, nome);

    enq = downloadManager.enqueue(request);
Run Code Online (Sandbox Code Playgroud)

在此之后,我的文件保存在目录文件夹中:Android >>内部共享存储>>下载.***我看到这条路径在ubuntu中手动打开设备的高清.由于图像显示路径. Android HD由ubuntu文件夹 - 查看路径

我尝试用这个打开这个文件:

downloadManager = (DownloadManager)getContext().getSystemService(DOWNLOAD_SERVICE);
        BroadcastReceiver receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                    DownloadManager.Query query = new DownloadManager.Query();
                    query.setFilterById(enq);
                    Cursor c = downloadManager.query(query);
                    if(c.moveToFirst()) {
                        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if(DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                            String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

                            if (uriString.substring(0, 7).matches("file://")) {
                                uriString =  uriString.substring(7);
                            }

                            File file = new File(uriString);

                            Uri uriFile = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
                            String mimetype = "application/pdf";
                            Intent myIntent = new Intent(Intent.ACTION_VIEW);
                            myIntent.setDataAndType(uriFile, mimetype);

                            Intent intentChooser = Intent.createChooser(myIntent, "Choose Pdf Application");
                            startActivity(intentChooser);
                        }
                    }
                }
            }
        };

        getContext().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Run Code Online (Sandbox Code Playgroud)

我在清单中声明我的文件提供程序:

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
Run Code Online (Sandbox Code Playgroud)

并与此:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="Download" path="Download/"/>
</paths>
Run Code Online (Sandbox Code Playgroud)

但是,当我单击按钮下载时,我收到此消息:"此文件无法访问.请检查位置或网络,然后重试."

恢复:

1 - 文件已下载并保存在目录文件夹中.

2 - 意图已启动,但文件未打开.

3 - 调试模式在"new File(urlString)"中给我这个:"urlString =/storage/emulated/0/Download/name.pdf"

4 - 在"FileProvider.getUriFromFile ..."调试模式下有:

"uriFile = content://com.example.android.parlamentaresapp.fileprovider/Download/name.pdf"

谢谢.

Com*_*are 18

打电话addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)Intent是您使用startActivity()FileProvider Uri.没有它,活动无权访问您的内容.