Example usage for android.content Context DOWNLOAD_SERVICE

List of usage examples for android.content Context DOWNLOAD_SERVICE

Introduction

In this page you can find the example usage for android.content Context DOWNLOAD_SERVICE.

Prototype

String DOWNLOAD_SERVICE

To view the source code for android.content Context DOWNLOAD_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.DownloadManager for requesting HTTP downloads.

Usage

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 av a  2s . c o m*/

    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

/**
 * Attempts to remove the download with the given ID from the system's download manager, if
 * available.//from  ww  w .  j  av a  2 s .co  m
 * 
 * @see {@link DownloadManager#remove(long...)}
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public static void removeFromDownloadManager(Context context, long id) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
        DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        if (downloadManager != null) {
            downloadManager.remove(id);
        }
    }
}

From source file:Main.java

/**
 * Attempts to notify the system's download manager, if available, that a file has been
 * downloaded./*from w w w .j a va2 s  . c o m*/
 * 
 * @see {@link DownloadManager#addCompletedDownload(String, String, boolean, String, String, long, boolean)}
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public static long addToDownloadManager(Context context, String title, String description,
        boolean isMediaScannerScannable, String mimeType, String path, long length, boolean showNotification) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
        DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        if (downloadManager != null) {
            return downloadManager.addCompletedDownload(title, description, isMediaScannerScannable, mimeType,
                    path, length, showNotification);
        }
    }
    return 0L;
}

From source file:Main.java

public static void downloadFile(Context context, String fileurl) {
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileurl));
    request.setDestinationInExternalPublicDir("/Download/", fileurl.substring(fileurl.lastIndexOf("/") + 1));
    DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    downloadManager.enqueue(request);/* ww  w  . ja  va2  s  .  c o m*/
}

From source file:Main.java

/**
 * Checks whether the download service is present on the device.
 * @param ctx The context of the application.
 * @return True if the download service can be used to download files.
 *//*ww w. j  a  va2 s.  c  om*/
public static boolean check_download_service(Context ctx) {
    if (_DOWNLOAD_SERVICE_AVAILABLE != null) {
        return _DOWNLOAD_SERVICE_AVAILABLE;
    }
    _DOWNLOAD_SERVICE_AVAILABLE = ctx.getSystemService(Context.DOWNLOAD_SERVICE) != null;
    return _DOWNLOAD_SERVICE_AVAILABLE;
}

From source file:Main.java

/**
 * Cancel download or erase already downloaded file for the specified content.
 * @param context any application context.
 * @param downloadId download identifier.
 *//*  w w  w .ja  va  2s  .  c  o m*/
public static void deleteDownload(Context context, long downloadId) {
    DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    downloadManager.remove(downloadId);
}

From source file:Main.java

/**
 * Save a media URI into the download directory
 * @param context the context//w w w.j  a  v a2 s  . c  o  m
 * @param srcFile the source file.
 * @param filename the filename (optional)
 * @return the downloads file path
 */
@SuppressLint("NewApi")
public static String saveMediaIntoDownloads(Context context, File srcFile, String filename, String mimeType) {
    String fullFilePath = saveFileInto(context, srcFile, Environment.DIRECTORY_DOWNLOADS, filename);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        if (null != fullFilePath) {
            DownloadManager downloadManager = (DownloadManager) context
                    .getSystemService(Context.DOWNLOAD_SERVICE);

            try {
                File file = new File(fullFilePath);
                downloadManager.addCompletedDownload(file.getName(), file.getName(), true, mimeType,
                        file.getAbsolutePath(), file.length(), true);
            } catch (Exception e) {
            }
        }
    }

    return fullFilePath;
}

From source file:de.escoand.readdaily.DownloadHandler.java

public static long startInvisibleDownload(final Context context, final String url, final String title) {
    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    String name = String.valueOf(new Random().nextInt());

    LogHandler.log(Log.WARN, "load invisible " + url);

    long id = manager.enqueue(new DownloadManager.Request(Uri.parse(url)).setTitle(title));
    Database.getInstance(context).addDownload(name, id, null);

    return id;/* ww w.  j  a v a2s.c o m*/
}

From source file:Main.java

/**
 * Note: Make sure isDownloadManagerAvailable return is true before use this method.
 * @param apkName Apk File Name/*from   w w w.  ja  v  a2s  .  co  m*/
 * @param fullApkUrl url of full
 * @param context Context
 */
public static void downloadApkByDownloadManager(String apkName, String fullApkUrl, Context context) {
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fullApkUrl));
    request.setDescription(fullApkUrl);
    request.setTitle(apkName);

    // in order for this if to run, you must use the android 3.2 to compile your app
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, apkName);
    request.setVisibleInDownloadsUi(false);
    request.setMimeType("application/vnd.android.package-archive");

    // get download service and enqueue file
    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);
}

From source file:Main.java

/**
 * Decode downloaded big picture.//from  w w  w.jav  a  2 s.  c o  m
 * @param context any application context.
 * @param downloadId downloaded picture identifier.
 * @return decoded bitmap if successful, null on error.
 */
public static Bitmap getBigPicture(Context context, long downloadId) {
    /* Decode bitmap */
    InputStream stream = null;
    try {
        /*
         * Query download manager to get file. FIXME For an unknown reason, using the file descriptor
         * fails after APK build with ProGuard, we use stream instead.
         */
        DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        Uri uri = downloadManager.getUriForDownloadedFile(downloadId);
        ContentResolver contentResolver = context.getContentResolver();
        stream = contentResolver.openInputStream(uri);

        /*
         * Bitmaps larger than 2048 in any dimension are likely to cause OutOfMemoryError in the
         * NotificationManager or even here. Plus some devices simply don't accept such images: the
         * notification manager can drop the notification without any way to check that via API. To
         * avoid the problem, sub sample the image in an efficient way (not using Bitmap matrix
         * scaling after decoding the bitmap with original size: it could run out of memory when
         * decoding the full image). FIXME There is center cropping applied by the NotificationManager
         * on the bitmap we provide, we can't avoid it, see
         * https://code.google.com/p/android/issues/detail?id=58318.
         */
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inSampleSize = 1;
        options.inPreferQualityOverSpeed = true;

        /* Decode dimensions */
        BitmapFactory.decodeStream(stream, null, options);
        int maxDim = Math.max(options.outWidth, options.outHeight);

        /* Compute sub sample size (it must be a power of 2) */
        while (maxDim > 2048) {
            options.inSampleSize <<= 1;
            maxDim >>= 1;
        }

        /* Decode actual bitmap */
        options.inJustDecodeBounds = false;
        stream.close();
        stream = contentResolver.openInputStream(uri);
        return BitmapFactory.decodeStream(stream, null, options);
    } catch (Throwable t) {
        /* Abort, causes are likely FileNotFoundException or OutOfMemoryError */
        return null;
    } finally {
        /* Silently close stream */
        if (stream != null)
            try {
                stream.close();
            } catch (IOException e) {
            }
    }
}