如何在android上以编程方式从下载管理器中保存sd卡上的下载文件

时间:2012-09-12 07:59:35

标签: android android-sdcard android-internet

在我的应用程序中,我有下载(图像)功能,用于从网址下载文件。下载发生应该显示在通知栏中,以便我使用Download Manager类下载文件。这工作正常,但下载的图像不存储在SD卡中的任何位置。

我已将url提交给下载管理员。

我的要求是我需要将下载图像保存到带有通知栏指示的SD卡。 要修改代码以在上述链接上的SD卡上保存图像

我对上述链接中的代码有疑问是否可以使用相同的代码下载音频或视频文件?

请帮帮我。

编辑问题:

我试过了

        filepath = Environment.getExternalStorageDirectory().getPath()+"/download/cm.png";
        Uri destinationUri = Uri.parse(filepath);
        request.setDestinationUri(destinationUri);
按钮上的首选项管理器点击之前

。但我无法在SD卡上获取该文件。

3 个答案:

答案 0 :(得分:12)

这就是我使用的。

        Uri downloadUri = Uri.parse(DOWNLOAD_FILE);
        DownloadManager.Request request = new DownloadManager.Request(downloadUri);
        request.setDescription("Downloading a file");
        long id =  downloadManager.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false)
                .setTitle("File Downloading...")
                .setDescription("Image File Download")
                .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "cm.png"));

答案 1 :(得分:2)

在您引用的代码中,文件最后打开。此时,您可以考虑将其复制到SD卡。

否则(更好)使用http://developer.android.com/reference/android/app/DownloadManager.Request.html setDestinationUri(android.net.Uri)指定要下载文件的位置。

答案 2 :(得分:0)

    downloadmanager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Toast.makeText(context, "Downloading...", Toast.LENGTH_LONG).show();
    Uri uri = Uri.parse("---- url here ------");
    request = new DownloadManager.Request(uri);
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    request.setAllowedOverRoaming(false);
    request.setTitle("---- title here ------");
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl("---- url here ------");
    request.setMimeType(mimeType);
    request.setDescription("---- descripation here ------");
    if("---- titlehere ------" != null){
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "---- title here ------");
    }

    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    Long reference = downloadmanager.enqueue(request);
相关问题