Android DownloadManager - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android DownloadManager class.

This code example shows how to use the following methods:getMimeTypeForDownloadedFile, getUriForDownloadedFile
This code example shows how to use the following methods:enqueue, setDestinationInExternalPublicDir
 
        // Check whether we have permission to write file to external storage. 
        // We only start download request if we have permission. 
        if (!checkWriteExternalPermission()) return; 
 
        Request request = new Request(Uri.parse(url));
        request.setDestinationInExternalPublicDir(
                Environment.DIRECTORY_DOWNLOADS, fileName);
        getDownloadManager().enqueue(request);
        popupMessages(DOWNLOAD_START_TOAST + fileName);
    } 
 
    private String getFileName(String url, String contentDisposition, String mimetype) {
        String fileName = URLUtil.guessFileName(url, contentDisposition, mimetype);
        int extensionIndex = fileName.lastIndexOf(".");
        String extension = null;
        if ((extensionIndex > 1) && (extensionIndex < fileName.length())) {
            extension = fileName.substring(extensionIndex + 1);
        } 
 
6
CodeRank
This code example shows how to use the following methods:getMimeTypeForDownloadedFile, getUriForDownloadedFile
This code example shows how to use the following methods:enqueue
        } else { 
            final DownloadManager manager
                    = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
            new Thread("Browser download") {
                public void run() { 
                    manager.enqueue(request);
                } 
            }.start();
        } 
        Toast.makeText(activity, R.string.download_pending, Toast.LENGTH_SHORT)
                .show(); 
    } 
 
} 
6
CodeRank
This code example shows how to use the following methods:enqueue
    } 
 
    public void testDownloadManagerRequest() { 
        // first try a minimal request 
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://localhost/path"));
        mManager.enqueue(request);
 
        // now set everything we can, save for external destintion (for which we lack permission) 
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        request.setAllowedOverRoaming(false);
        request.setTitle("test");
        request.setDescription("test");
        request.setMimeType("text/html");
        request.addRequestHeader("X-Some-Header", "value");
        mManager.enqueue(request);
    } 
} 
6
CodeRank
This code example shows how to use the following methods:enqueue
			// notify user when download is completed 
			request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
			 
			// start download 
			DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
			manager.enqueue(request);
		} 
		else 
		{ 
			try 
			{ 
				if(url!=null)
				{ 
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
					context.startActivity(intent);
				} 
			} 
			catch(android.content.ActivityNotFoundException e)
			{ 
				// can't start activity 
5
CodeRank
This code example shows how to use the following methods:getUriForDownloadedFile, query
                                     id, getDownloadFailureReason(id)));
            callback.onError();
            return; 
        } 
 
        Uri uri = downloadManager.getUriForDownloadedFile(id);
        Log.v(TAG, String.format("File %s downloaded successfully", uri.getPath()));
 
        callback.onFinish(uri);
    } 
 
    private boolean isDownloadSuccessful(long id) {
        int status = getDownloadQueryAsInt(id, DownloadManager.COLUMN_STATUS);
        return (DownloadManager.STATUS_SUCCESSFUL == status);
    } 
 
    private int getDownloadFailureReason(long id) {
        return getDownloadQueryAsInt(id, DownloadManager.COLUMN_REASON);
    } 
 

5
CodeRank
This code example shows how to use the following methods:query
This code example shows how to use the following methods:enqueue
    protected DownloadManager.Request getRequest(String path) {
        return new DownloadManager.Request(Uri.parse(path));
    } 
 
    protected Download enqueueRequest(DownloadManager.Request request) {
        return new Download(mManager.enqueue(request));
    } 
} 
5
CodeRank
This code example shows how to use the following methods:enqueue
    //request.setDestinationInExternalPublicDir("XCSoarData", path); 
    request.setDestinationUri(Uri.fromFile(new File(path)));
 
    request.setAllowedOverRoaming(false);
    request.setShowRunningNotification(true);
    return dm.enqueue(request);
  } 
 
  /** 
   * Find a download with the specified name.  Returns -1 if none was 
   * found. 
   */ 
  static long findPath(DownloadManager dm, String path) {
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterByStatus(DownloadManager.STATUS_PAUSED |
                            DownloadManager.STATUS_PENDING |
                            DownloadManager.STATUS_RUNNING);
    Cursor c = dm.query(query);
 
    if (!c.moveToFirst())
5
CodeRank

Related Android DownloadManager Questions & Answers:

See Code Examples for Android DownloadManager Methods: