Android下载管理器

问题描述 投票:5回答:3

我想我有一个相当简单的问题。

http://www.vogella.com/blog/2011/06/14/android-downloadmanager-example/

我一直在关注上面的网址中的教程。

如何更改下载的文件路径?

提前致谢

android file path filepath download-manager
3个回答
18
投票

您可以使用这种信息配置DownloadManager.Request对象。在本教程中,Request对象是在onClick()中创建和使用的。

例如:

DownloadManager.Request req=new DownloadManager.Request(uri);

req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
                               | DownloadManager.Request.NETWORK_MOBILE)
   .setAllowedOverRoaming(false)
   .setTitle("Demo")
   .setDescription("Something useful. No, really.")
   .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                                      "test.mp4");

(以上代码来自this sample project


5
投票

CommonsWare的答案中的最后一行说明了目的地。他只是使用SD卡上的常规download-folder,但你也可以这样做:

req.setDestinationInExternalPublicDir("/mnt/sdcard/Myfolder", "file_name.extension");

0
投票

您确保用户允许您的应用进行外部存储访问。包含此代码以供下载

DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
     Uri uri = Uri.parse(url_string);
     DownloadManager.Request request = new DownloadManager.Request(uri);        
     request.setVisibleInDownloadsUi(true);        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    // include this line if permission has been granted by user to external directory
     request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());  
// or this line if not yet granted
request.setDestinationUri(Uri.parse("file://" + uri.getLastPathSegment());      
    Long reference = downloadManager.enqueue(request); 
© www.soinside.com 2019 - 2024. All rights reserved.