Search code examples
androidandroid-download-manager

Different value for 'get/setDestination' Directory


Tested in Emulator

At first I make a directory in this form :

            File root = android.os.Environment.getExternalStorageDirectory();
            File dir = new File(root.getAbsolutePath() + "/" + "SubFolderName");
            if (!dir.exists()) {
                dir.mkdirs();

That make directory in this path : /storage/emulated/0/SubFolderName

Now, I need to use this path for downloaded file, so add this code:

 DownloadManager.Request request = new DownloadManager.Request(uri);
 request.setDestinationInExternalPublicDir(root.getAbsolutePath() + "/" + "SubFolderName","FileName");

That return this path: /storage/emulated/0/storage/emulated/0/SubFolderName/

Too try before this:

 request.setDestinationInExternalFilesDir(getActivity(), root.getAbsolutePath() + "/" + "SubFolderName","FileName");

This section are repeated : storage/emulated/0

I check my codes but there-are not mistake.


Solution

  • You are not specifying which directory in external storage.

    There are several directories you can use depending on what you are storing e.g "Downloads", "Video"

    You can use the downloads directory like this for example:

    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs();
    
    Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS + "/" + "SubFolderName/" + mTedListModel.get(position).getTitle()).mkdirs();
    

    And then set your destination with the request manager as

      .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, + "SubFolderName/" + mTedListModel.get(position).getTitle());