DownloadManager
public
class
DownloadManager
extends Object
java.lang.Object | |
↳ | android.app.DownloadManager |
The download manager is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file. The download manager will conduct the download in the background, taking care of HTTP interactions and retrying downloads after failures or across connectivity changes and system reboots.
Apps that request downloads through this API should register a broadcast receiver for
ACTION_NOTIFICATION_CLICKED
to appropriately handle when the user clicks on a running
download in a notification or from the downloads UI.
Note that the application must have the Manifest.permission.INTERNET
permission to use this class.
Summary
Nested classes | |
---|---|
class |
DownloadManager.Query
This class may be used to filter download manager queries. |
class |
DownloadManager.Request
This class contains all the information necessary to request a new download. |
Constants | |
---|---|
String |
ACTION_DOWNLOAD_COMPLETE
Broadcast intent action sent by the download manager when a download completes. |
String |
ACTION_NOTIFICATION_CLICKED
Broadcast intent action sent by the download manager when the user clicks on a running download, either from a system notification or from the downloads UI. |
String |
ACTION_VIEW_DOWNLOADS
Intent action to launch an activity to display all downloads. |
String |
COLUMN_BYTES_DOWNLOADED_SO_FAR
Number of bytes download so far. |
String |
COLUMN_DESCRIPTION
The client-supplied description of this download. |
String |
COLUMN_ID
An identifier for a particular download, unique across the system. |
String |
COLUMN_LAST_MODIFIED_TIMESTAMP
Timestamp when the download was last modified, in |
String |
COLUMN_LOCAL_FILENAME
This constant was deprecated
in API level 24.
apps should transition to using
|
String |
COLUMN_LOCAL_URI
Uri where downloaded file will be stored. |
String |
COLUMN_MEDIAPROVIDER_URI
The URI to the corresponding entry in MediaProvider for this downloaded entry. |
String |
COLUMN_MEDIA_TYPE
Internet Media Type of the downloaded file. |
String |
COLUMN_REASON
Provides more detail on the status of the download. |
String |
COLUMN_STATUS
Current status of the download, as one of the STATUS_* constants. |
String |
COLUMN_TITLE
The client-supplied title for this download. |
String |
COLUMN_TOTAL_SIZE_BYTES
Total size of the download in bytes. |
String |
COLUMN_URI
URI to be downloaded. |
int |
ERROR_CANNOT_RESUME
Value of |
int |
ERROR_DEVICE_NOT_FOUND
Value of |
int |
ERROR_FILE_ALREADY_EXISTS
Value of |
int |
ERROR_FILE_ERROR
Value of |
int |
ERROR_HTTP_DATA_ERROR
Value of |
int |
ERROR_INSUFFICIENT_SPACE
Value of |
int |
ERROR_TOO_MANY_REDIRECTS
Value of |
int |
ERROR_UNHANDLED_HTTP_CODE
Value of |
int |
ERROR_UNKNOWN
Value of COLUMN_ERROR_CODE when the download has completed with an error that doesn't fit under any other error code. |
String |
EXTRA_DOWNLOAD_ID
Intent extra included with |
String |
EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS
When clicks on multiple notifications are received, the following provides an array of download ids corresponding to the download notification that was clicked. |
String |
INTENT_EXTRAS_SORT_BY_SIZE
Intent extra included with |
int |
PAUSED_QUEUED_FOR_WIFI
Value of |
int |
PAUSED_UNKNOWN
Value of |
int |
PAUSED_WAITING_FOR_NETWORK
Value of |
int |
PAUSED_WAITING_TO_RETRY
Value of |
int |
STATUS_FAILED
Value of |
int |
STATUS_PAUSED
Value of |
int |
STATUS_PENDING
Value of |
int |
STATUS_RUNNING
Value of |
int |
STATUS_SUCCESSFUL
Value of |
Public methods | |
---|---|
long
|
addCompletedDownload(String title, String description, boolean isMediaScannerScannable, String mimeType, String path, long length, boolean showNotification)
This method was deprecated
in API level 29.
Apps should instead contribute files to
|
long
|
addCompletedDownload(String title, String description, boolean isMediaScannerScannable, String mimeType, String path, long length, boolean showNotification, Uri uri, Uri referer)
This method was deprecated
in API level 29.
Apps should instead contribute files to
|
long
|
enqueue(DownloadManager.Request request)
Enqueue a new download. |
static
Long
|
getMaxBytesOverMobile(Context context)
Returns maximum size, in bytes, of downloads that may go over a mobile connection; or null if there's no limit |
String
|
getMimeTypeForDownloadedFile(long id)
Returns the media type of the given downloaded file id, if the file was downloaded successfully. |
static
Long
|
getRecommendedMaxBytesOverMobile(Context context)
Returns recommended maximum size, in bytes, of downloads that may go over a mobile connection; or null if there's no recommended limit. |
Uri
|
getUriForDownloadedFile(long id)
Returns the |
ParcelFileDescriptor
|
openDownloadedFile(long id)
Open a downloaded file for reading. |
Cursor
|
query(DownloadManager.Query query)
Query the download manager about downloads that have been requested. |
int
|
remove(long... ids)
Cancel downloads and remove them from the download manager. |
Inherited methods | |
---|---|
Constants
ACTION_DOWNLOAD_COMPLETE
public static final String ACTION_DOWNLOAD_COMPLETE
Broadcast intent action sent by the download manager when a download completes.
Constant Value: "android.intent.action.DOWNLOAD_COMPLETE"
ACTION_NOTIFICATION_CLICKED
public static final String ACTION_NOTIFICATION_CLICKED
Broadcast intent action sent by the download manager when the user clicks on a running download, either from a system notification or from the downloads UI.
Constant Value: "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"
ACTION_VIEW_DOWNLOADS
public static final String ACTION_VIEW_DOWNLOADS
Intent action to launch an activity to display all downloads.
Constant Value: "android.intent.action.VIEW_DOWNLOADS"
COLUMN_BYTES_DOWNLOADED_SO_FAR
public static final String COLUMN_BYTES_DOWNLOADED_SO_FAR
Number of bytes download so far.
Constant Value: "bytes_so_far"
COLUMN_DESCRIPTION
public static final String COLUMN_DESCRIPTION
The client-supplied description of this download. This will be displayed in system notifications. Defaults to the empty string.
Constant Value: "description"
COLUMN_ID
public static final String COLUMN_ID
An identifier for a particular download, unique across the system. Clients use this ID to make subsequent calls related to the download.
Constant Value: "_id"
COLUMN_LAST_MODIFIED_TIMESTAMP
public static final String COLUMN_LAST_MODIFIED_TIMESTAMP
Timestamp when the download was last modified, in System.currentTimeMillis()
(wall clock time in UTC).
Constant Value: "last_modified_timestamp"
COLUMN_LOCAL_FILENAME
public static final String COLUMN_LOCAL_FILENAME
This constant was deprecated
in API level 24.
apps should transition to using
ContentResolver.openFileDescriptor(Uri, String)
instead.
Path to the downloaded file on disk.
Note that apps may not have filesystem permissions to directly access
this path. Instead of trying to open this path directly, apps should use
ContentResolver.openFileDescriptor(Uri, String)
to gain access.
Constant Value: "local_filename"
COLUMN_LOCAL_URI
public static final String COLUMN_LOCAL_URI
Uri where downloaded file will be stored. If a destination is supplied by client, that URI will be used here. Otherwise, the value will initially be null and will be filled in with a generated URI once the download has started.
Constant Value: "local_uri"
COLUMN_MEDIAPROVIDER_URI
public static final String COLUMN_MEDIAPROVIDER_URI
The URI to the corresponding entry in MediaProvider for this downloaded entry. It is used to delete the entries from MediaProvider database when it is deleted from the downloaded list.
Constant Value: "mediaprovider_uri"
COLUMN_MEDIA_TYPE
public static final String COLUMN_MEDIA_TYPE
Internet Media Type of the downloaded file. If no value is provided upon creation, this will initially be null and will be filled in based on the server's response once the download has started.
See also:
Constant Value: "media_type"
COLUMN_REASON
public static final String COLUMN_REASON
Provides more detail on the status of the download. Its meaning depends on the value of
COLUMN_STATUS
.
When COLUMN_STATUS
is STATUS_FAILED
, this indicates the type of error that
occurred. If an HTTP error occurred, this will hold the HTTP status code as defined in RFC
2616. Otherwise, it will hold one of the ERROR_* constants.
When COLUMN_STATUS
is STATUS_PAUSED
, this indicates why the download is
paused. It will hold one of the PAUSED_* constants.
If COLUMN_STATUS
is neither STATUS_FAILED
nor STATUS_PAUSED
, this
column's value is undefined.
See also:
Constant Value: "reason"
COLUMN_STATUS
public static final String COLUMN_STATUS
Current status of the download, as one of the STATUS_* constants.
Constant Value: "status"
COLUMN_TITLE
public static final String COLUMN_TITLE
The client-supplied title for this download. This will be displayed in system notifications. Defaults to the empty string.
Constant Value: "title"
COLUMN_TOTAL_SIZE_BYTES
public static final String COLUMN_TOTAL_SIZE_BYTES
Total size of the download in bytes. This will initially be -1 and will be filled in once the download starts.
Constant Value: "total_size"
COLUMN_URI
public static final String COLUMN_URI
URI to be downloaded.
Constant Value: "uri"
ERROR_CANNOT_RESUME
public static final int ERROR_CANNOT_RESUME
Value of COLUMN_REASON
when some possibly transient error occurred but we can't
resume the download.
Constant Value: 1008 (0x000003f0)
ERROR_DEVICE_NOT_FOUND
public static final int ERROR_DEVICE_NOT_FOUND
Value of COLUMN_REASON
when no external storage device was found. Typically,
this is because the SD card is not mounted.
Constant Value: 1007 (0x000003ef)
ERROR_FILE_ALREADY_EXISTS
public static final int ERROR_FILE_ALREADY_EXISTS
Value of COLUMN_REASON
when the requested destination file already exists (the
download manager will not overwrite an existing file).
Constant Value: 1009 (0x000003f1)
ERROR_FILE_ERROR
public static final int ERROR_FILE_ERROR
Value of COLUMN_REASON
when a storage issue arises which doesn't fit under any
other error code. Use the more specific ERROR_INSUFFICIENT_SPACE
and
ERROR_DEVICE_NOT_FOUND
when appropriate.
Constant Value: 1001 (0x000003e9)
ERROR_HTTP_DATA_ERROR
public static final int ERROR_HTTP_DATA_ERROR
Value of COLUMN_REASON
when an error receiving or processing data occurred at
the HTTP level.
Constant Value: 1004 (0x000003ec)
ERROR_INSUFFICIENT_SPACE
public static final int ERROR_INSUFFICIENT_SPACE
Value of COLUMN_REASON
when there was insufficient storage space. Typically,
this is because the SD card is full.
Constant Value: 1006 (0x000003ee)
ERROR_TOO_MANY_REDIRECTS
public static final int ERROR_TOO_MANY_REDIRECTS
Value of COLUMN_REASON
when there were too many redirects.
Constant Value: 1005 (0x000003ed)
ERROR_UNHANDLED_HTTP_CODE
public static final int ERROR_UNHANDLED_HTTP_CODE
Value of COLUMN_REASON
when an HTTP code was received that download manager
can't handle.
Constant Value: 1002 (0x000003ea)
ERROR_UNKNOWN
public static final int ERROR_UNKNOWN
Value of COLUMN_ERROR_CODE when the download has completed with an error that doesn't fit under any other error code.
Constant Value: 1000 (0x000003e8)
EXTRA_DOWNLOAD_ID
public static final String EXTRA_DOWNLOAD_ID
Intent extra included with ACTION_DOWNLOAD_COMPLETE
intents, indicating the ID (as a
long) of the download that just completed.
Constant Value: "extra_download_id"
EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS
public static final String EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS
When clicks on multiple notifications are received, the following
provides an array of download ids corresponding to the download notification that was
clicked. It can be retrieved by the receiver of this
Intent using Intent.getLongArrayExtra(String)
.
Constant Value: "extra_click_download_ids"
INTENT_EXTRAS_SORT_BY_SIZE
public static final String INTENT_EXTRAS_SORT_BY_SIZE
Intent extra included with ACTION_VIEW_DOWNLOADS
to start DownloadApp in
sort-by-size mode.
Constant Value: "android.app.DownloadManager.extra_sortBySize"
PAUSED_QUEUED_FOR_WIFI
public static final int PAUSED_QUEUED_FOR_WIFI
Value of COLUMN_REASON
when the download exceeds a size limit for downloads over
the mobile network and the download manager is waiting for a Wi-Fi connection to proceed.
Constant Value: 3 (0x00000003)
PAUSED_UNKNOWN
public static final int PAUSED_UNKNOWN
Value of COLUMN_REASON
when the download is paused for some other reason.
Constant Value: 4 (0x00000004)
PAUSED_WAITING_FOR_NETWORK
public static final int PAUSED_WAITING_FOR_NETWORK
Value of COLUMN_REASON
when the download is waiting for network connectivity to
proceed.
Constant Value: 2 (0x00000002)
PAUSED_WAITING_TO_RETRY
public static final int PAUSED_WAITING_TO_RETRY
Value of COLUMN_REASON
when the download is paused because some network error
occurred and the download manager is waiting before retrying the request.
Constant Value: 1 (0x00000001)
STATUS_FAILED
public static final int STATUS_FAILED
Value of COLUMN_STATUS
when the download has failed (and will not be retried).
Constant Value: 16 (0x00000010)
STATUS_PAUSED
public static final int STATUS_PAUSED
Value of COLUMN_STATUS
when the download is waiting to retry or resume.
Constant Value: 4 (0x00000004)
STATUS_PENDING
public static final int STATUS_PENDING
Value of COLUMN_STATUS
when the download is waiting to start.
Constant Value: 1 (0x00000001)
STATUS_RUNNING
public static final int STATUS_RUNNING
Value of COLUMN_STATUS
when the download is currently running.
Constant Value: 2 (0x00000002)
STATUS_SUCCESSFUL
public static final int STATUS_SUCCESSFUL
Value of COLUMN_STATUS
when the download has successfully completed.
Constant Value: 8 (0x00000008)
Public methods
addCompletedDownload
public long addCompletedDownload (String title, String description, boolean isMediaScannerScannable, String mimeType, String path, long length, boolean showNotification)
This method was deprecated
in API level 29.
Apps should instead contribute files to
MediaStore.Downloads
collection to make them available to user
as part of Downloads.
Adds a file to the downloads database system, so it could appear in Downloads App (and thus become eligible for management by the Downloads App).
It is helpful to make the file scannable by MediaScanner by setting the param isMediaScannerScannable to true. It makes the file visible in media managing applications such as Gallery App, which could be a useful purpose of using this API.
For applications targeting Build.VERSION_CODES.Q
or above,
path
must be within directories owned by the application
{e.g. Context.getExternalFilesDir(String)
} or if the application is running under
the legacy storage model (see
android:requestLegacyExternalStorage
), path
can also be within the top-level
Downloads directory (as returned by
Environment.getExternalStoragePublicDirectory(String)
with
Environment.DIRECTORY_DOWNLOADS
).
Parameters | |
---|---|
title |
String : the title that would appear for this file in Downloads App. |
description |
String : the description that would appear for this file in Downloads App. |
isMediaScannerScannable |
boolean : true if the file is to be scanned by MediaScanner. Files
scanned by MediaScanner appear in the applications used to view media (for example,
Gallery app). |
mimeType |
String : mimetype of the file. |
path |
String : absolute pathname to the file. The file should be world-readable, so that it can
be managed by the Downloads App and any other app that is used to read it (for example,
Gallery app to display the file, if the file contents represent a video/image). |
length |
long : length of the downloaded file |
showNotification |
boolean : true if a notification is to be sent, false otherwise |
Returns | |
---|---|
long |
an ID for the download entry added to the downloads app, unique across the system This ID is used to make future calls related to this download. |
addCompletedDownload
public long addCompletedDownload (String title, String description, boolean isMediaScannerScannable, String mimeType, String path, long length, boolean showNotification, Uri uri, Uri referer)
This method was deprecated
in API level 29.
Apps should instead contribute files to
MediaStore.Downloads
collection to make them available to user
as part of Downloads.
Adds a file to the downloads database system, so it could appear in Downloads App (and thus become eligible for management by the Downloads App).
It is helpful to make the file scannable by MediaScanner by setting the param isMediaScannerScannable to true. It makes the file visible in media managing applications such as Gallery App, which could be a useful purpose of using this API.
For applications targeting Build.VERSION_CODES.Q
or above,
path
must be within directories owned by the application
{e.g. Context.getExternalFilesDir(String)
} or if the application is running under
the legacy storage model (see
android:requestLegacyExternalStorage
), path
can also be within the top-level
Downloads directory (as returned by
Environment.getExternalStoragePublicDirectory(String)
with
Environment.DIRECTORY_DOWNLOADS
).
Parameters | |
---|---|
title |
String : the title that would appear for this file in Downloads App. |
description |
String : the description that would appear for this file in Downloads App. |
isMediaScannerScannable |
boolean : true if the file is to be scanned by MediaScanner. Files
scanned by MediaScanner appear in the applications used to view media (for example,
Gallery app). |
mimeType |
String : mimetype of the file. |
path |
String : absolute pathname to the file. The file should be world-readable, so that it can
be managed by the Downloads App and any other app that is used to read it (for example,
Gallery app to display the file, if the file contents represent a video/image). |
length |
long : length of the downloaded file |
showNotification |
boolean : true if a notification is to be sent, false otherwise |
uri |
Uri : the original HTTP URI of the download |
referer |
Uri : the HTTP Referer for the download |
Returns | |
---|---|
long |
an ID for the download entry added to the downloads app, unique across the system This ID is used to make future calls related to this download. |
enqueue
public long enqueue (DownloadManager.Request request)
Enqueue a new download. The download will start automatically once the download manager is ready to execute it and connectivity is available.
Parameters | |
---|---|
request |
DownloadManager.Request : the parameters specifying this download |
Returns | |
---|---|
long |
an ID for the download, unique across the system. This ID is used to make future calls related to this download. Returns -1 if the operation fails. |
getMaxBytesOverMobile
public static Long getMaxBytesOverMobile (Context context)
Returns maximum size, in bytes, of downloads that may go over a mobile connection; or null if there's no limit
Parameters | |
---|---|
context |
Context : the Context to use for accessing the ContentResolver |
Returns | |
---|---|
Long |
maximum size, in bytes, of downloads that may go over a mobile connection; or null if there's no limit |
getMimeTypeForDownloadedFile
public String getMimeTypeForDownloadedFile (long id)
Returns the media type of the given downloaded file id, if the file was downloaded successfully. Otherwise, null is returned.
Parameters | |
---|---|
id |
long : the id of the downloaded file. |
Returns | |
---|---|
String |
the media type of the given downloaded file id, if download was successful. null otherwise. |
getRecommendedMaxBytesOverMobile
public static Long getRecommendedMaxBytesOverMobile (Context context)
Returns recommended maximum size, in bytes, of downloads that may go over a mobile connection; or null if there's no recommended limit. The user will have the option to bypass this limit.
Parameters | |
---|---|
context |
Context : the Context to use for accessing the ContentResolver |
Returns | |
---|---|
Long |
recommended maximum size, in bytes, of downloads that may go over a mobile connection; or null if there's no recommended limit. |
getUriForDownloadedFile
public Uri getUriForDownloadedFile (long id)
Returns the Uri
of the given downloaded file id, if the file is
downloaded successfully. Otherwise, null is returned.
Parameters | |
---|---|
id |
long : the id of the downloaded file. |
Returns | |
---|---|
Uri |
the Uri of the given downloaded file id, if download was
successful. null otherwise. |
openDownloadedFile
public ParcelFileDescriptor openDownloadedFile (long id)
Open a downloaded file for reading. The download must have completed.
Parameters | |
---|---|
id |
long : the ID of the download |
Returns | |
---|---|
ParcelFileDescriptor |
a read-only ParcelFileDescriptor |
Throws | |
---|---|
FileNotFoundException |
if the destination file does not already exist |
query
public Cursor query (DownloadManager.Query query)
Query the download manager about downloads that have been requested.
Parameters | |
---|---|
query |
DownloadManager.Query : parameters specifying filters for this query |
Returns | |
---|---|
Cursor |
a Cursor over the result set of downloads, with columns consisting of all the COLUMN_* constants. |
remove
public int remove (long... ids)
Cancel downloads and remove them from the download manager. Each download will be stopped if it was running, and it will no longer be accessible through the download manager. If there is a downloaded file, partial or complete, it is deleted.
Parameters | |
---|---|
ids |
long : the IDs of the downloads to remove |
Returns | |
---|---|
int |
the number of downloads actually removed |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-12-18 UTC.