Aravind Srivatsan
Aravind Srivatsan

Reputation: 435

Android Download Manager setDestinationInExternalFilesDir

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?

Upvotes: 17

Views: 20870

Answers (5)

Aliton Oliveira
Aliton Oliveira

Reputation: 1339

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/"

Upvotes: 1

Grafritz Design
Grafritz Design

Reputation: 728

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

Upvotes: 0

Grafritz Design
Grafritz Design

Reputation: 728

Add the permission in manifest file

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

Upvotes: 2

Pandiri Deepak
Pandiri Deepak

Reputation: 176

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

this worked for me..

Upvotes: 1

MDMalik
MDMalik

Reputation: 4001

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.

Upvotes: 1

Related Questions