4

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());
Surajkumar_cse
  • 165
  • 1
  • 11
  • Maybe `android:requestLegacyExternalStorage="true"` in the `` element of the manifest? – CoolMind Oct 18 '20 at 20:17
  • @CoolMind, This has no effect in android11 – Surajkumar_cse Oct 19 '20 at 01:05
  • I haven't faced the situation, please, see https://stackoverflow.com/questions/64230839/java-lang-illegalargumentexception-unknown-uri-when-trying-to-open-downloaded-i. Maybe an error in `FileProvider` or `res/xml/file_provider_paths.xml` file? You can also change a directory as advised in https://stackoverflow.com/questions/64016682/how-to-create-folder-android-r-api-30 (also PICTURES and so on). – CoolMind Oct 19 '20 at 06:05
  • 1
    An interesting approach to get people to switch to opening a pdf file with the Google Drive PDF Viewer instead of using the tool we want to use (e.g. Adobe Acrobat). Thanks Google! – jrw32982 Nov 03 '20 at 17:59

2 Answers2

1

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);
Johana Lopez 1327
  • 165
  • 1
  • 3
  • 14
  • 1
    i resolved it by removing download manager and using HTTPUrlConnection to download. For Android 10, you can use android:requestLegacyExternalStorage="true" in android manifest. – Surajkumar_cse Feb 02 '21 at 14:33
  • It seems that is the only possible solution. But it is always important to have in mind that `android:requestLegacyExternalStorage=true` is ignored in Android 11, so it needs to be handled in a proper way – Johana Lopez 1327 Feb 02 '21 at 15:04
1

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.

pintu236
  • 148
  • 2
  • 11