0

I am using download manager to download MP3 Files from my server.

Here is the code for it.

public String createFilePath()
    {
        String path;
        String dir  = "APP_NAME";

        path = Environment.getExternalStorageDirectory().getPath();



        File file = new File(Environment.getExternalStorageDirectory(),dir);
        if(!file.exists())
        {
            file.mkdir();
        }

        path +=   "/" +dir + "/";

        System.out.println("-- saving path : " + path);
        return path;
    }

    public void startDownload() {
        Uri uri=Uri.parse(URLFixer.Fix(DATA.url));




        System.out.println("-- download path : " + createFilePath() + FileNameGetter.getFileName(DATA..url));

        DownloadManager.Request request = new Request(uri);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE);
        request.setAllowedOverRoaming(false);
        request.setTitle(DATA..title);
        request.setDescription(DATA.artist + " - " + DATA.album);


        request.setDestinationInExternalFilesDir(activity, createFilePath(), FileNameGetter.getFileName(DATA..url));
        //      request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);




        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
            //                  request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
            request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }


        lastDownload=   mgr.enqueue(request);
        isDownloading = true;
        Toasts.pop(activity, "Download Started!!");

        //      v.setEnabled(false);
        //      findViewById(R.id.query).setEnabled(true);
    }

The problem is that it should be saved on SD Card inside folder "APP_NAME", but once the audio is downloaded, I cant see it inside that folder, and when I play the audio, and check its info, it shows path like thie

/sdcard/Android/data/com.X.app/files/mnt/sdcard/APP_NAME/file.mp3

As its being saved inside data folder, user is not able to see the file. How to fix it to move it to main SD Card i.e. /mnt/sdcard/APP_NAME so that user can see it.

Adil Bhatty
  • 17,190
  • 34
  • 81
  • 118

1 Answers1

0

DownloadManager.Request.setDestinationUri(Uri uri) should fit your requirement, remember to call allowScanningByMediaScanner() if you want the MP3 to be scanned by MediaScanner.

Kai
  • 15,284
  • 6
  • 51
  • 82