【问题标题】:How to save downloaded file on sd card from download manager programmatically on android如何在 android 上以编程方式从下载管理器将下载的文件保存在 sd 卡上
【发布时间】:2012-09-12 07:59:34
【问题描述】:

在我的应用程序中,我有下载(图像)功能,用于从 url 下载文件。下载发生应显示在通知栏中,以便我使用下载管理器类下载文件。这工作正常,但下载的图像没有存储在 sdcard 中的任何位置。

我已将url 推荐给下载管理器。

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

我对上面链接中的代码有一些疑问,我可以使用相同的代码下载音频或视频文件吗?

请帮帮我。

已编辑问题:

我试过了

        filepath = Environment.getExternalStorageDirectory().getPath()+"/download/cm.png";
        Uri destinationUri = Uri.parse(filepath);
        request.setDestinationUri(destinationUri);

在按钮点击偏好管理器之前。但我无法在 sdcard 上获取文件。

【问题讨论】:

  • 只要是http下载就可以下载任何类型的文件
  • 谢谢,但我怎样才能从代码中保存内容?
  • 默认它们保存在缓存文件夹中。正如 njzk2 指出的那样,您必须使用 setDestinationUri 、 setDestinationInExternalPublicDir 或 setDestinationInExternalFilesDir
  • this 帮忙吗?
  • @nandeesh,请回答我编辑的问题

标签: android android-sdcard android-internet


【解决方案1】:

这是我用的。

        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"));

【讨论】:

  • 我认为这行不通?它会将您的文件保存到手机内存上的默认下载文件夹中,而不是保存在您的存储卡上?还有一件事 long id = downloadManager.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE) .setAllowedOverRoaming(false) .setTitle("文件下载...") .setDescription("图片文件下载") .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "cm.png")); ==> enqueue() 函数返回长值? ???
  • 如果设备没有 sd 卡,setDestinationInExternalPublicDir 会起作用吗?
【解决方案2】:

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

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

【讨论】:

  • 我已经尝试并在按钮单击时使用了 setDestination(...) 但我无法获得。请查看我编辑的问题。
  • 这行不通。使用 Uri.fromFile 而不是 Uri.parse 除非您在路径的开头添加 file://
  • 我试过 filepath = Environment.getExternalStorageDirectory().getPath()+"/cm.png";文件 fileNew = 新文件(文件路径); Uri destinationUri = Uri.fromFile(fileNew);但仍然没有保存在 sdcard 上。
  • 最后我得到了 setDestinationInExternalPublic。请看我的回答。谢谢你的帮助。
【解决方案3】:
    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);

【讨论】:

  • 试着解释你做了什么以及你是怎么做的。有很多代码,没有解释的答案不好。
猜你喜欢
  • 2014-11-05
  • 1970-01-01
  • 2010-12-15
  • 2014-05-13
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 2023-03-14
  • 2013-04-13
相关资源
最近更新 更多