如何在安卓系统上以编程方式从下载管理器将下载的文件保存在sd卡上



在我的应用程序中,我有下载(图像)功能,用于从URL下载文件。下载发生时应该显示在通知栏中,以便我使用下载管理器类下载文件。这运行良好,但下载的图像不会存储在SD卡中的任何位置。

我引用了下载管理器的url

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

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

请帮帮我。

编辑问题:

我试过

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

在首选项管理器单击按钮之前。但是我在sdcard上找不到文件。

这就是我使用的。

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

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

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

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

最新更新