List of usage examples for android.app DownloadManager enqueue
public long enqueue(Request request)
From source file:Main.java
public static void addImage(final Context context, final String url, final String fileName) { if (!getAppDownloadsDirectory().exists()) { getAppDownloadsDirectory().mkdirs(); }// ww w . j a va2s. com DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setAllowedNetworkTypes( DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE) .setAllowedOverRoaming(true).setTitle("Pics-on-Tumblr") .setDescription("Pics-on-Tumblr is saving picture to your device") .setDestinationInExternalPublicDir(DOWNLOADS_DIRECTORY, fileName).setVisibleInDownloadsUi(false) .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); downloadManager.enqueue(request); }
From source file:Main.java
/** * @param path the absolute destination path *//*from w ww . j a va2s.c o m*/ static long enqueue(DownloadManager dm, String uri, String path) { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(uri)); /* Unfortunately, we cannot use the simpler "ExternalPublicDir" Android feature, because on some Samsung products, we need to explicitly use the "external_sd" mount instead of the built-in storage. Here, we must use whatever was returned by FindDataPath() in LocalPath.cpp. */ //request.setDestinationInExternalPublicDir("XCSoarData", path); request.setDestinationUri(Uri.fromFile(new File(path))); request.setAllowedOverRoaming(false); request.setShowRunningNotification(true); return dm.enqueue(request); }
From source file:com.ruesga.rview.misc.ActivityHelper.java
@SuppressWarnings("ResultOfMethodCallIgnored") public static void downloadUri(Context context, Uri uri, String fileName, @Nullable String mimeType) { // Create the destination location File destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); destination.mkdirs();/* www . j a v a 2 s . c om*/ Uri destinationUri = Uri.fromFile(new File(destination, fileName)); // Use the download manager to perform the download DownloadManager downloadManager = (DownloadManager) context.getSystemService(Activity.DOWNLOAD_SERVICE); Request request = new Request(uri).setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) .setDestinationUri(destinationUri); if (mimeType != null) { request.setMimeType(mimeType); } request.allowScanningByMediaScanner(); downloadManager.enqueue(request); }
From source file:com.android.xbrowser.FetchUrlMimeType.java
@Override public void run() { // User agent is likely to be null, though the AndroidHttpClient // seems ok with that. AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent); HttpHost httpHost = Proxy.getPreferredHttpHost(mContext, mUri); if (httpHost != null) { ConnRouteParams.setDefaultProxy(client.getParams(), httpHost); }/*from w ww . j a v a2s . c o m*/ HttpHead request = new HttpHead(mUri); if (mCookies != null && mCookies.length() > 0) { request.addHeader("Cookie", mCookies); } HttpResponse response; String mimeType = null; String contentDisposition = null; try { response = client.execute(request); // We could get a redirect here, but if we do lets let // the download manager take care of it, and thus trust that // the server sends the right mimetype if (response.getStatusLine().getStatusCode() == 200) { Header header = response.getFirstHeader("Content-Type"); if (header != null) { mimeType = header.getValue(); final int semicolonIndex = mimeType.indexOf(';'); if (semicolonIndex != -1) { mimeType = mimeType.substring(0, semicolonIndex); } } Header contentDispositionHeader = response.getFirstHeader("Content-Disposition"); if (contentDispositionHeader != null) { contentDisposition = contentDispositionHeader.getValue(); } } } catch (IllegalArgumentException ex) { request.abort(); } catch (IOException ex) { request.abort(); } finally { client.close(); } if (mimeType != null) { if (mimeType.equalsIgnoreCase("text/plain") || mimeType.equalsIgnoreCase("application/octet-stream")) { String newMimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(mUri)); if (newMimeType != null) { mRequest.setMimeType(newMimeType); } } String filename = URLUtil.guessFileName(mUri, contentDisposition, mimeType); mRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); } // Start the download DownloadManager manager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(mRequest); }
From source file:com.mogoweb.browser.FetchUrlMimeType.java
@Override public void run() { // User agent is likely to be null, though the AndroidHttpClient // seems ok with that. AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent); // HttpHost httpHost; // try { // httpHost = Proxy.getPreferredHttpHost(mContext, mUri); // if (httpHost != null) { // ConnRouteParams.setDefaultProxy(client.getParams(), httpHost); // } // } catch (IllegalArgumentException ex) { // Log.e(LOGTAG,"Download failed: " + ex); // client.close(); // return; // }/*from ww w . jav a 2s. c om*/ HttpHead request = new HttpHead(mUri); if (mCookies != null && mCookies.length() > 0) { request.addHeader("Cookie", mCookies); } HttpResponse response; String mimeType = null; String contentDisposition = null; try { response = client.execute(request); // We could get a redirect here, but if we do lets let // the download manager take care of it, and thus trust that // the server sends the right mimetype if (response.getStatusLine().getStatusCode() == 200) { Header header = response.getFirstHeader("Content-Type"); if (header != null) { mimeType = header.getValue(); final int semicolonIndex = mimeType.indexOf(';'); if (semicolonIndex != -1) { mimeType = mimeType.substring(0, semicolonIndex); } } Header contentDispositionHeader = response.getFirstHeader("Content-Disposition"); if (contentDispositionHeader != null) { contentDisposition = contentDispositionHeader.getValue(); } } } catch (IllegalArgumentException ex) { request.abort(); } catch (IOException ex) { request.abort(); } finally { client.close(); } if (mimeType != null) { if (mimeType.equalsIgnoreCase("text/plain") || mimeType.equalsIgnoreCase("application/octet-stream")) { String newMimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(mUri)); if (newMimeType != null) { mimeType = newMimeType; mRequest.setMimeType(newMimeType); } } String filename = URLUtil.guessFileName(mUri, contentDisposition, mimeType); mRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); } // Start the download DownloadManager manager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(mRequest); }
From source file:com.bluros.updater.service.DownloadService.java
private long enqueueDownload(String downloadUrl, String localFilePath) { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadUrl)); String userAgent = Utils.getUserAgentString(this); if (userAgent != null) { request.addRequestHeader("User-Agent", userAgent); }// w ww .j a v a 2 s . com request.setTitle(getString(R.string.app_name)); request.setDestinationUri(Uri.parse(localFilePath)); request.setAllowedOverRoaming(false); request.setVisibleInDownloadsUi(false); // TODO: this could/should be made configurable request.setAllowedOverMetered(true); final DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); return dm.enqueue(request); }
From source file:com.tcity.android.ui.info.BuildArtifactsFragment.java
@Override public void onDownloadClick(@NotNull BuildArtifact artifact) { //noinspection ResultOfMethodCallIgnored Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs(); DownloadManager manager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(calculateArtifactRequest(artifact)); }
From source file:com.otaupdater.utils.KernelInfo.java
@TargetApi(11) public long fetchFile(Context ctx) { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setTitle(ctx.getString(R.string.notif_downloading)); request.setDescription(kernelName);/* w w w . j a v a2 s .c o m*/ request.setVisibleInDownloadsUi(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); } request.setDestinationUri(Uri.fromFile(new File(Config.KERNEL_DL_PATH_FILE, getDownloadFileName()))); int netTypes = DownloadManager.Request.NETWORK_WIFI; if (!Config.getInstance(ctx).getWifiOnlyDl()) netTypes |= DownloadManager.Request.NETWORK_MOBILE; request.setAllowedNetworkTypes(netTypes); DownloadManager manager = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE); return manager.enqueue(request); }
From source file:com.otaupdater.utils.RomInfo.java
@TargetApi(11) public long fetchFile(Context ctx) { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setTitle(ctx.getString(R.string.notif_downloading)); request.setDescription(romName);/*from w w w . j a v a 2 s.co m*/ request.setVisibleInDownloadsUi(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); } request.setDestinationUri(Uri.fromFile(new File(Config.ROM_DL_PATH_FILE, getDownloadFileName()))); int netTypes = DownloadManager.Request.NETWORK_WIFI; if (!Config.getInstance(ctx).getWifiOnlyDl()) netTypes |= DownloadManager.Request.NETWORK_MOBILE; request.setAllowedNetworkTypes(netTypes); DownloadManager manager = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE); return manager.enqueue(request); }
From source file:com.workingagenda.democracydroid.Adapters.ViewHolders.EpisodeViewHolder.java
@RequiresApi(api = Build.VERSION_CODES.M) private void Download(String url, String title, String desc) { if (ContextCompat.checkSelfPermission(itemView.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ((Activity) itemView.getContext()) .requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0); // TODO: catch onRequestPermissionsResult } else {/* w w w .j ava2 s .c om*/ if ("http://democracynow.videocdn.scaleengine.net/democracynow-iphone/play/democracynow/playlist.m3u8" .equals(url)) { Toast toast = Toast.makeText(itemView.getContext(), "You can't download the Live Stream", Toast.LENGTH_LONG); toast.show(); return; } DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setDescription(desc); request.setTitle(title); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); String fileext = url.substring(url.lastIndexOf('/') + 1); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PODCASTS, fileext); //http://stackoverflow.com/questions/24427414/getsystemservices-is-undefined-when-called-in-a-fragment // get download service and enqueue file DownloadManager manager = (DownloadManager) itemView.getContext() .getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(request); // TODO: Save que ID for cancel button Toast toast = Toast.makeText(itemView.getContext(), "Starting download of " + title, Toast.LENGTH_LONG); toast.show(); } }