I wasn't facing this issue in Android 9 and in Android 10 i opted out for Legacy Storage. But in Android 11, I am not able to open the file downloaded from Download Manager. The same file can be opened from file Manager, though.

Also, I checked the owner of that file, it is showing com.android.providers.downloads and not my app.

I am also creating a pdf file in Document/myfolder directory, and it can be easily openable using intent and there my app is the owner of that file.

Also, if I am saving the downloaded file to Document/myfolder directory, it still can't be opened using that intent.

As per my understanding, from Android 11, they introduced scope storage, and if the owner of that file is accessing it. It will allow to read/write through READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permission.

For both the file, I am using the same Intent.

Anyone has idea how to change the owner of my downloaded file so that i can gain open or what is the exact reason i am not able to open that downloaded file?

 Intent intent = new Intent(Intent.ACTION_VIEW);

 intent.setDataAndType(uri, PDF_TYPE);

 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);

 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

Here PDF_TYPE is correct and uri is also correct.

DownloadManager code is also correct here a small snippet

                        .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, subPath.getPath());
2

There are 2 answers

2
Johana Lopez 1327 On

I am having the same issue, but in Android 10.

This works for me in Android 11

Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file);
0
Saurabh On

I was facing the same issue even though I was saving my files to downloads directory. When I register a receiver and open it after download is completed then it's working fine with below code

    private void openPdf(File file) {

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri uri = FileProvider.getUriForFile(BudgetBookActivity.this, "com.demo.fmsapp", file);

    intent.setDataAndType(uri, "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    try {

        startActivity(Intent.createChooser(intent, "Open File"));
    } catch (ActivityNotFoundException e) {
        Toast.makeText(BudgetBookActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Log.d(BudgetBookActivity.class.getSimpleName(), "Some error for budget" + e.getMessage());
    }
}

and this is my download code I was using

                    dialog = ProgressDialog.show(activity(), "", "Downloading file please wait", true);

                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

                registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

                request.setTitle(fileName);
                request.setDescription("Budget Volume PDF");
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,fileName);
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);


                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                downloadID = manager.enqueue(request);

I was getting uri from the download id using this : Uri uri = dm.getUriForDownloadedFile(downloadID); then I get file from URI then pass it to openPdf method but the problem was that it wasn't working when I am trying to access it by this when I only had filename

                File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),fileName);

so what I unexpectedly did is just add a subpath to the download directory when saving and accessing file like this

private static final String APP_DOWNLOAD_PATH = "/test/";    
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, APP_DOWNLOAD_PATH + fileName);

File file = new
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),APP_DOWNLOAD_PATH + fileName);

So I am using file provider so you need to configure same first. It's working fine.I don't why it's not working when saving to download folder itself though.