23 Dec 2020

How To Download Files with DownloadManager.class?


how to download files with Android DownloadManager.class
How to download files with Download Manager class?
Hi guys, How are you?
In this post we shall try to create a simple application's code which will help you to Download Files. For Instance we shall use system predefined android DownloadManager.class. That works is to Download the Files.

Here see main DownloadManager.class & its Two Inner/Neasted Classes. public class DownloadManager extend Object{ public static class DownloadManager.Query extends Object{ } public static class DownloadManager.Request extends Object{ } }

DownloadManager

DownloadManager is used to Download content / file of long running 🏃 HTTP connection.

DownloadManager.Query

This DownloadManager.Query Neasted class helps you to filter downlods queues by 5 types of status. these five status are given below.

  • DownloadManager.STATUS_FAILED
  • DownloadManager.STATUS_PAUSED
  • DownloadManager.STATUS_PENDING
  • DownloadManager.STATUS_RUNNING
  • DownloadManager.STATUS_SUCCESSFUL

Required Permission


<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

DownloadManager Codes



DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

        Request request = new Request(Uri.parse(DOWNLOAD_URL));

        //Get download file name
        String fileExtenstion = MimeTypeMap.getFileExtensionFromUrl(DOWNLOAD_URL);
        String name = URLUtil.guessFileName(DOWNLOAD_URL, null, fileExtenstion);

        //Save file to destination folder
        request.setDestinationInExternalPublicDir("/Downloads", name);

        enqueue = downloadManager.enqueue(request);
    }


Reaction:

No comments:

No Comments

COMMENT FROM MESSAGE