Search code examples
androidandroid-download-manager

Unable to download files using download manager in emulator


Android Studio 2.1.2, API 23

    Error:

java.lang.SecurityException: No permission to write to/storage/emulated/0/Download/aabd.pdf: Neither user 10059 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.

Code :

     File file = new File(Environment.getExternalStoragePublicDirectory
            (Environment.DIRECTORY_DOWNLOADS), nameOfFile);
   request.setDestinationInExternalPublicDir
  (Environment.DIRECTORY_DOWNLOADS, nameOfFile);
   request.setVisibleInDownloadsUi(true);
   myDownloadReference = downloadManager.enqueue(request);

In the devices, it is working fine.

In Manifest permission is there

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.player">


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Solution

  • You have to check permission like this if you have targetSdk 23

        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
            checkPermission();
        }
        else {
    
        File file = new File(Environment.getExternalStoragePublicDirectory
            (Environment.DIRECTORY_DOWNLOADS), nameOfFile);
                request.setDestinationInExternalPublicDir
            (Environment.DIRECTORY_DOWNLOADS, nameOfFile);
            request.setVisibleInDownloadsUi(true);
            myDownloadReference = downloadManager.enqueue(request);
        }
    
    
    private void checkPermission() {
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {//Can add more as per requirement
    
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},
                            123);
    
                } else {
    
                }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 123: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
             File file = new File(Environment.getExternalStoragePublicDirectory
            (Environment.DIRECTORY_DOWNLOADS), nameOfFile);
                request.setDestinationInExternalPublicDir
            (Environment.DIRECTORY_DOWNLOADS, nameOfFile);
            request.setVisibleInDownloadsUi(true);
            myDownloadReference = downloadManager.enqueue(request);
    
                } else {
    
                    checkPermission();
                }
                return;
            }
        }
    }