FileProvider - open a file from the download directory

I can not open the file from the download folder.

I can download the file and save it in the download folder with this:

   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);

After that, my file is correctly saved in the Directory Folder: Android >> Internal shared storage >> Download. *** This way I see by manually opening the HD device in Ubuntu. As the image shows the way. Android HD using ubuntu folder - see path

And I am trying to open this file with this:

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));

I declare my file provider in the manifest with this:

    <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>

and with this:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="Download" path="Download/"/>
</paths>

But when I click the download button, I get this message: "This file is not available. Check the location or network and try again."

In summary:

1 - the file is downloaded and saved in the directory folder.

2 - Intention is running, but the file is not open.

3 - " (urlString)": "urlString =/storage/emulated/0/Download/name.pdf"

4 - "FileProvider.getUriFromFile..." :

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

.

+14
1

addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) Intent, startActivity() FileProvider Uri. .

+18

Source: https://habr.com/ru/post/1673540/


All Articles