1

I updated Android SDK TO 30 Then Issues occurred while downloading the pdf file from api.

At SDK 29
Uri uri1 = Uri.fromFile(newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), ""));

Above code was working fine. After upgrading to SDK 30 following code doesn't show the downloaded file.

My Code

        File direct2 = new File(getExternalFilesDir(null)+ FileUtils.DOWNLOAD_PDF_DESTINAION);

        if (!direct2.exists()) {
            direct2.mkdirs();
        }

        DownloadManager downloadManager = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(uri);

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
            Uri uri1 = Uri.fromFile(new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), ""));
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                    .setAllowedOverRoaming(true).setTitle(title + strDate + ".pdf")
                    .setDescription(description)
                    .setDestinationUri(uri1)
                    .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);//to show the DOWNLOAD notification when completed
        } ```

  • Can you confirm is you are using scoped storage or is requestLegacyExternalStorage=true is this added in your AndroidManifest.xml file. If above key is added it won't work as this has been deprecated in api 30 [link](https://stackoverflow.com/questions/63364476/requestlegacyexternalstorage-is-not-working-in-android-11-api-30) – Nitish Sep 08 '21 at 07:53
  • I have added requestLegacyExternalStorage= true in manifest and it was still not working. Do I need to use both Scoped storage and requestLegacyExternalStorage or any one? – Anish Maharjan Sep 08 '21 at 08:24
  • If you update sdk 30 , requestLegacyExternalStorage won't work in android 10 and 11. You have to upgrade to scope storage. requestLegacyExternalStorage= true , only works for sdk 29 – Nitish Sep 08 '21 at 08:26
  • 1
    I tried to use scope storage. File is created but its size was 0B. I need to download PDF file from Server. How can I use it? – Anish Maharjan Sep 08 '21 at 09:21
  • You can try looking at how file download works in android 11 [link](https://github.com/jignesh8992/Android-11-Permissiond-And-Download-Manager) If you still faces the issue, please update the code so that we can look for the issue together – Nitish Sep 08 '21 at 10:19

0 Answers0