androidandroid-sdcardandroid-internet

How to save downloaded file on sd card from download manager programmatically on android


In my application i have download(Image) feature which is used to download file from urls. The download happen should be shown in notification bar so that i used Download Manager class to download file. This is working fine but the downloaded image does not stored no where in the sdcard.

i have referred the url for the download manager.

my requirement is i need to save the download image to sdcard with notification bar indication. What to modify on the code to get save image on sdcard on the above link

i have some doubts regards the code in the above link is Can i use the same code to download audio or video file?

please help me.

Edited question:

I have tried

        filepath = Environment.getExternalStorageDirectory().getPath()+"/download/cm.png";
        Uri destinationUri = Uri.parse(filepath);
        request.setDestinationUri(destinationUri);

before the preference manger on the button click. but i could not get the file on sdcard.


Solution

  • This is what i used.

            Uri downloadUri = Uri.parse(DOWNLOAD_FILE);
            DownloadManager.Request request = new DownloadManager.Request(downloadUri);
            request.setDescription("Downloading a file");
            long id =  downloadManager.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE)
                    .setAllowedOverRoaming(false)
                    .setTitle("File Downloading...")
                    .setDescription("Image File Download")
                    .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "cm.png"));