17

I am using the Download Manager and when I use

 setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, "example.ext");

the files are downloaded to Android/data/com.example.app/files/Download folder.

When I try

setDestinationInExternalPublicDir("/folder", "example.ext");

I get: IllegalStateException: Cannot create directory mnt/sdcard/folder. I've set the WRITE_EXTERNAL_STORAGE permission too.

What am I doing wrong?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Aravind Srivatsan
  • 435
  • 1
  • 5
  • 15

5 Answers5

2

Add the permission in manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Grafritz Design
  • 677
  • 6
  • 7
1

Why don't you use absolute path for ExternalFileDir

File sdCard = Environment.getExternalStorageDirectory();
String folder = sdCard.getAbsolutePath() + "/YourFolder" ;
File dir = new File(folder );
if (!dir.exists()) {
if (dir.mkdirs()) {
    Log.i(Tag,"Directory  Created");
    }
}

I guess this might even work for you.

MDMalik
  • 3,951
  • 2
  • 25
  • 39
  • I tried the following. downloadRequest.setDestinationInExternalFilesDir(v.getContext(), folder, down.getTitle()+".epub")); But the file gets downloaded to file:///mnt/sdcard/Android/data/com.example.app/files/*folder-path* – Aravind Srivatsan May 27 '13 at 12:40
  • 1
    I temporarily fixed it by moving the file after downloading from the Android/data/com.example.app/files to the directory I required. – Aravind Srivatsan May 27 '13 at 15:10
  • I fixed my problem by using this command: request.setDestinationUri(Uri.fromFile(new File(destinationDirectory,fileName + fileExtension))); Where destinationDirectory is String destinationDirectory = Environment.getExternalStorageDirectory() + "/MyFolder/" – Aliton Oliveira Jan 24 '19 at 23:13
1

use request.setDestinationInExternalPublicDir("/folder", "FileName.extention");

this worked for me..

MikroDel
  • 6,705
  • 7
  • 39
  • 74
Pandiri Deepak
  • 176
  • 3
  • 15
  • 1
    Probably just because my version is much more recent than this post, but I had to add the context, so request.setDestinationInExternalPublicDir(this, "/folder", "Filename.extension"); – ConcernedHobbit Nov 03 '19 at 22:54
1

I fixed my problem by using this command:

request.setDestinationUri(Uri.fromFile(new File(destinationDirectory,fileName + fileExtension))); 

Where destinationDirectory is

String destinationDirectory = Environment.getExternalStorageDirectory() + "/MyFolder/"
Aliton Oliveira
  • 1,224
  • 1
  • 15
  • 26
  • this giving me this error https://stackoverflow.com/questions/67932843/w-downloadmanager-aborting-request-for-download-17-failed-to-create-target-fil?noredirect=1#comment120073691_67932843 – Sachin Burdak Jun 11 '21 at 08:35
0

Check the permission like

//region [ PERMISSION ]

public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean CheckPermission(final Context context){
    int currentAPIVersion = Build.VERSION.SDK_INT;
    if( currentAPIVersion >= Build.VERSION_CODES.M){
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
                alertBuilder.setCancelable(true);
                alertBuilder.setTitle("Permission necessary");
                alertBuilder.setMessage("External storage permission is necessary");
                alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions((Activity) context,
                                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
                    }
                });
                AlertDialog alert = alertBuilder.create();
                alert.show();
            } else {
                ActivityCompat.requestPermissions((Activity) context,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
            }
            return false;
        } else {
            return true;
        }
    } else {
        return true;
    }
}

//endregion
Grafritz Design
  • 677
  • 6
  • 7