无法在 DownloadManager 类中动态更改下载文件的名称

易卜拉欣

我正在使用下载管理器类下载 Mp3 文件。

    DownloadManager downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
//dls is an arraylist that holds the download links
                Uri uri=Uri.parse(dls.get(0));
                DownloadManager.Request request= new DownloadManager.Request(uri);

                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"file.mp3");
                downloadManager.enqueue(request);

setDestinationInExternalPublicDir方法需要第二个参数来更改下载文件的名称。

我希望文件具有其默认名称。如果我不使用该方法,该文件将具有其默认名称,但它不会位于下载目录中。

我怎样才能实现这两个目标,在下载目录中找到文件并保留文件名?

感谢帮助。

阿卜杜拉·阿卡姆

你能试试这个吗:

public static String getFileNameFromURL(String url) {
if (url == null) {
    return "";
}
try {
    URL resource = new URL(url);
    String host = resource.getHost();
    if (host.length() > 0 && url.endsWith(host)) {
        // handle ...example.com
        return "";
    }
}
catch(MalformedURLException e) {
    return "";  
}

int startIndex = url.lastIndexOf('/') + 1;
int length = url.length();

// find end index for ?
int lastQMPos = url.lastIndexOf('?');
if (lastQMPos == -1) {
    lastQMPos = length; 
}

    // find end index for #
    int lastHashPos = url.lastIndexOf('#');
    if (lastHashPos == -1) {
    lastHashPos = length;   
    }

    // calculate the end index
     int endIndex = Math.min(lastQMPos, lastHashPos);
    return url.substring(startIndex, endIndex);
}

此方法可以处理以下类型的输入:

Input: "null" Output: ""
Input: "" Output: ""
Input: "file:///home/user/test.html" Output: "test.html"
Input: "file:///home/user/test.html?id=902" Output: "test.html"
Input: "file:///home/user/test.html#footer" Output: "test.html"
Input: "http://example.com" Output: ""
Input: "http://www.example.com" Output: ""
Input: "http://www.example.txt" Output: ""
Input: "http://example.com/" Output: ""
Input: "http://example.com/a/b/c/test.html" Output: "test.html"
Input: "http://example.com/a/b/c/test.html?param=value" Output: "test.html"
Input: "http://example.com/a/b/c/test.html#anchor" Output: "test.html"
Input: "http://example.com/a/b/c/test.html#anchor?param=value" Output: "test.html"

你可以在这里找到完整的源代码:https : //ideone.com/uFWxTL

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

DownloadManager无法开始下载文件

来自分类Dev

在Android中无法通过DownloadManager下载文件

来自分类Dev

DownloadManager完成下载后无法移动文件

来自分类Dev

无法使用DownloadManager从名称为国际字符的URL下载文件

来自分类Dev

Android DownloadManager将文件保存到设备,无法打开

来自分类Dev

DownloadManager setVisibleInDownloadsUi无法正常工作

来自分类Dev

DownloadManager setVisibleInDownloadsUi无法正常工作

来自分类Dev

不能通过Android中的DownloadManager下载文件

来自分类Dev

下载管理器在android.app.downloadmanager中不是公开的。无法从外部包访问

来自分类Dev

Android:DownloadManager是作为FIFO还是同时下载文件

来自分类Dev

设置android的DownloadManager类的代理

来自分类Dev

DownloadManager不在下载文件夹中存储下载的文件

来自分类Dev

DownloadManager不在下载文件夹中存储下载的文件

来自分类Dev

如何使用Downloadmanager类在/ databases文件夹中下载文件?

来自分类Dev

Android DownloadManager不保存文件

来自分类Dev

DownloadManager下载超过2.1 GB的文件

来自分类Dev

通过DownloadManager下载后打开apk文件

来自分类Dev

Android:如何引用DownloadManager下载的文件

来自分类Dev

在Android中从downloadManager查找下载完成

来自分类Dev

在Android中从downloadManager查找下载完成

来自分类Dev

使用Android中的DownloadManager类进行多次下载

来自分类Dev

使用DownloadManager下载HTTP / HTTPS URI

来自分类Dev

DownloadManager 下载进度不可见

来自分类Dev

将类扩展到DownloadManager

来自分类Dev

Android DownloadManager 类:getUriForDownloadedFile 返回错误路径

来自分类Dev

DownloadManager IllegalStateException在DIRECTORY_DOWNLOADS中创建下载

来自分类Dev

无法使用javascript设置下载文件的下载名称

来自分类Dev

使用Python自动更改下载文件的名称

来自分类Dev

Android DownloadManager中止下载请求123:http错误206

Related 相关文章

  1. 1

    DownloadManager无法开始下载文件

  2. 2

    在Android中无法通过DownloadManager下载文件

  3. 3

    DownloadManager完成下载后无法移动文件

  4. 4

    无法使用DownloadManager从名称为国际字符的URL下载文件

  5. 5

    Android DownloadManager将文件保存到设备,无法打开

  6. 6

    DownloadManager setVisibleInDownloadsUi无法正常工作

  7. 7

    DownloadManager setVisibleInDownloadsUi无法正常工作

  8. 8

    不能通过Android中的DownloadManager下载文件

  9. 9

    下载管理器在android.app.downloadmanager中不是公开的。无法从外部包访问

  10. 10

    Android:DownloadManager是作为FIFO还是同时下载文件

  11. 11

    设置android的DownloadManager类的代理

  12. 12

    DownloadManager不在下载文件夹中存储下载的文件

  13. 13

    DownloadManager不在下载文件夹中存储下载的文件

  14. 14

    如何使用Downloadmanager类在/ databases文件夹中下载文件?

  15. 15

    Android DownloadManager不保存文件

  16. 16

    DownloadManager下载超过2.1 GB的文件

  17. 17

    通过DownloadManager下载后打开apk文件

  18. 18

    Android:如何引用DownloadManager下载的文件

  19. 19

    在Android中从downloadManager查找下载完成

  20. 20

    在Android中从downloadManager查找下载完成

  21. 21

    使用Android中的DownloadManager类进行多次下载

  22. 22

    使用DownloadManager下载HTTP / HTTPS URI

  23. 23

    DownloadManager 下载进度不可见

  24. 24

    将类扩展到DownloadManager

  25. 25

    Android DownloadManager 类:getUriForDownloadedFile 返回错误路径

  26. 26

    DownloadManager IllegalStateException在DIRECTORY_DOWNLOADS中创建下载

  27. 27

    无法使用javascript设置下载文件的下载名称

  28. 28

    使用Python自动更改下载文件的名称

  29. 29

    Android DownloadManager中止下载请求123:http错误206

热门标签

归档