Download File from URL - Android

In this part we are going to see how to download files(Images ,Video,Audio,PDF,etc) file from URL.



CREATE PATH IN EXTERNAL DIRECTORY:
File direct = new File(Environment.getExternalStorageDirectory()+ "/Demo Downloads");
Note: We use android default download manger to download files.
Check directory name is available or not:
if (!direct.exists()) {
direct.mkdirs();
}

Full Code:

url=http://i1-news.softpedia-static.com/images/news2/Google-Releases-Calendar-5-0-for-Android-with-Material-Design-UI-Screenshot-Tour-464126-12.jpg
void dwnladimage(String url) {


    String time_stamp = "" + utilHelper.GetDateTime(7);

    File direct = new File(Environment.getExternalStorageDirectory()
            + "/Filmogram Downloads");

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

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


    Uri downloadUri = Uri.parse(url); // specify the image url string here...."String img file"
    DownloadManager.Request request = new DownloadManager.Request(downloadUri);

    request.setAllowedNetworkTypes(
            DownloadManager.Request.NETWORK_WIFI                    | DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false).setTitle("Filmogram")
            .setDescription("Downloading Image" + time_stamp)
            .setDestinationInExternalPublicDir("/Filmogram", "Filmogram" + time_stamp + ".jpg");

    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    mgr.enqueue(request);
}

Comments

Popular posts from this blog

Kotlin - The official language for android development.

Free Push Notification using onesignal - Android Part 2

Android DatePicker Example