Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 479
M.A.Murali Avatar asked Dec 09 '22 21:12

M.A.Murali


2 Answers

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"));
like image 128
M.A.Murali Avatar answered Jan 12 '23 13:01

M.A.Murali


In the code you refer to, the file is opened at the end. At this point, you can consider copying it to the SDCard.

Otherwise (better) use http://developer.android.com/reference/android/app/DownloadManager.Request.html setDestinationUri(android.net.Uri) to specify where you want to download the file.

like image 28
njzk2 Avatar answered Jan 12 '23 15:01

njzk2