Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save file on SD card by using download manger in Android?

Tags:

android

I am facing tow issues in my app.

1- I am using Android DownloadManager to download online files but it downloads all files to system directories say Download directory by using

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, "fileName");

Now I want to download the file to my own directory say "New Folder". How is it possible to implement. Please help me in this respect, I would be very thankful.

2- How can I check that whether SD card is available or not?

like image 521
user1703737 Avatar asked Apr 30 '13 07:04

user1703737


People also ask

Can you download directly to SD card Android?

You can enable saving to your SD card using the Files app on the latest version of Android. If files do not save to the SD card automatically, you can transfer them to the SD card using the Files app on stock Android, or the My Files app on Samsung Galaxy.

Where does Download Manager save files Android?

You can find your downloads on your Android device in your My Files app (called File Manager on some phones), which you can find in the device's App Drawer. Unlike iPhone, app downloads are not stored on the home screen of your Android device, and can be found with an upward swipe on the home screen.


2 Answers

use this way: Here Dhaval_files is my folder name

public void file_download(String uRl) {
        File direct = new File(Environment.getExternalStorageDirectory()
                + "/dhaval_files");

        if (!direct.exists()) {
            direct.mkdirs();
        }

        DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);

        Uri downloadUri = Uri.parse(uRl);
        DownloadManager.Request request = new DownloadManager.Request(
                downloadUri);

        request.setAllowedNetworkTypes(
                DownloadManager.Request.NETWORK_WIFI
                        | DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false).setTitle("Demo")
                .setDescription("Something useful. No, really.")
                .setDestinationInExternalPublicDir("/dhaval_files", "test.jpg");

        mgr.enqueue(request);

    }
like image 185
Dhaval Parmar Avatar answered Sep 23 '22 17:09

Dhaval Parmar


DownloadManager

1)

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"));

2)

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

if(isSDPresent)
{
  // yes download code
}
else
{
 // No sdcard 
}
like image 33
Nirav Ranpara Avatar answered Sep 22 '22 17:09

Nirav Ranpara