4

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.

M.A.Murali
  • 9,988
  • 36
  • 105
  • 182

3 Answers3

12

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"));
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182
  • 2
    I don't think this would work? It will save your file into default Download folder on Phone memory rather than save on your memory card ? And one more thing 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")); ==> the enqueue() function return long value? ??? – toantran Nov 14 '13 at 07:16
  • will setDestinationInExternalPublicDir work if device doesn't have sd card ? – Elnoor Oct 08 '17 at 11:05
2

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.

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • i have tried and used the setDestination(...) on the button click but i could not get. please see my edited question. – M.A.Murali Sep 12 '12 at 08:30
  • 1
    this won't work. Use Uri.fromFile rather than Uri.parse unless you add file:// at the begining of the path – njzk2 Sep 12 '12 at 09:45
  • i have tried filepath = Environment.getExternalStorageDirectory().getPath()+"/cm.png"; File fileNew = new File(filepath); Uri destinationUri = Uri.fromFile(fileNew); But still no save on sdcard. – M.A.Murali Sep 12 '12 at 11:16
  • 2
    finally i got with setDestinationInExternalPublic. please see my answer. Thanks for you help. – M.A.Murali Sep 12 '12 at 12:08
0
    downloadmanager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Toast.makeText(context, "Downloading...", Toast.LENGTH_LONG).show();
    Uri uri = Uri.parse("---- url here ------");
    request = new DownloadManager.Request(uri);
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    request.setAllowedOverRoaming(false);
    request.setTitle("---- title here ------");
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl("---- url here ------");
    request.setMimeType(mimeType);
    request.setDescription("---- descripation here ------");
    if("---- titlehere ------" != null){
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "---- title here ------");
    }

    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    Long reference = downloadmanager.enqueue(request);