Java Code Examples for android.os.Environment.DIRECTORY_DOWNLOADS

The following are Jave code examples for showing how to use DIRECTORY_DOWNLOADS of the android.os.Environment class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: downloadmanager   File: Helpers.java   View Source Code Vote up 7 votes
private static File getDestinationDirectory(Context context, int destination, boolean running)
        throws IOException {
    switch (destination) {
        case Downloads.Impl.DESTINATION_CACHE_PARTITION:
        case Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE:
        case Downloads.Impl.DESTINATION_CACHE_PARTITION_NOROAMING:
            if (running) {
                return context.getFilesDir();
            } else {
                return context.getCacheDir();
            }
        case Downloads.Impl.DESTINATION_EXTERNAL:
            final File target = new File(
                    Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS);
            if (!target.isDirectory() && target.mkdirs()) {
                throw new IOException("unable to create external downloads directory");
            }
            return target;

        default:
            throw new IllegalStateException("unexpected destination: " + destination);
    }
}
 
Example 2
Project: Android-DownloadManager   File: Helpers.java   View Source Code Vote up 7 votes
private static File getDestinationDirectory(Context context, int destination, boolean running)
        throws IOException {
    switch (destination) {
        case Downloads.Impl.DESTINATION_CACHE_PARTITION:
        case Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE:
        case Downloads.Impl.DESTINATION_CACHE_PARTITION_NOROAMING:
            if (running) {
                return context.getFilesDir();
            } else {
                return context.getCacheDir();
            }
        case Downloads.Impl.DESTINATION_EXTERNAL:
            final File target = new File(
                    Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS);
            if (!target.isDirectory() && target.mkdirs()) {
                throw new IOException("unable to create external downloads directory");
            }
            return target;

        default:
            throw new IllegalStateException("unexpected destination: " + destination);
    }
}
 
Example 3
Project: wulkanowy   File: Updater.java   View Source Code Vote up 6 votes
private void startDownload() {
    Snackbar.make(activity.findViewById(R.id.fragment_container), "Downloading started.", Snackbar.LENGTH_SHORT).show();

    String path = Environment.getExternalStorageDirectory().toString() + File.separator +
            Environment.DIRECTORY_DOWNLOADS + File.separator + "wulkanowy";

    File dir = new File(path);
    if(!dir.mkdirs()) {
        for (String aChildren : dir.list()) {
            new File(dir, aChildren).delete();
        }
    }

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(update.getUrlToDownload().toString()))
            .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false)
            .setTitle("Wulkanowy v" + update.getLatestVersionCode())
            .setDescription(update.getLatestVersion())
            .setVisibleInDownloadsUi(true)
            .setMimeType("application/vnd.android.package-archive")
            .setDestinationUri(Uri.fromFile(new File(path + File.separator + update.getLatestVersion() + ".apk")));

    downloadManager.enqueue(request);
}
 
Example 4
Project: microbit   File: FileUtils.java   View Source Code Vote up 5 votes
private static void dirChecker(String dir) {
    File f = new File(Environment.DIRECTORY_DOWNLOADS + dir);

    if(!f.isDirectory()) {
        f.mkdirs();
    }
}
 
Example 5
Project: Cable-Android   File: StorageUtil.java   View Source Code Vote up 4 votes
public static File getDownloadDir() throws NoExternalStorageException {
  return new File(getSignalStorageDir(), Environment.DIRECTORY_DOWNLOADS);
}
 
Example 6
Project: Android-DownloadManager   File: MainActivity.java   View Source Code Vote up 4 votes
@Override
public void onClick(View v) {

    if (v.getId() == R.id.download) {

        checkPermiss();

        if (isDownloading) {
            Toast.makeText(MainActivity.this, "心急吃不了豆腐", Toast.LENGTH_SHORT).show();
            return;
        }

        DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://download.taobaocdn.com/wireless/taobao4android/latest/701483.apk"));
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
        request.setTitle("下载jpg");
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        request.setMimeType("image/jpeg");

        //实际下载后存放的路径并不一定是这个名字,如果有重名的,自动向名字中追加数字编号
        File apkFile = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS + "/701483.apk");
        request.setDestinationUri(Uri.fromFile(apkFile));

        downloadId = dw.enqueue(request);
        isDownloading = true;

        Uri uri = dw.getDownloadUri(downloadId);

        if (uri != null) {
            progressBar.setMax(1000);
            progressBar.setProgress(0);

            if (observer != null) {
                getContentResolver().unregisterContentObserver(observer);
                observer = null;
            }
            observer = new DownloadStatusObserver();
            getContentResolver().registerContentObserver(uri, true, observer);
        }
    } else if (v.getId() == R.id.pause) {
        dw.pauseDownload(downloadId);
    } else if (v.getId() == R.id.resume) {
        dw.resumeDownload(downloadId);
    }

}