使用DownloadManager下载HTTP/HTTPS URI

8

我在谷歌上搜索了一些关于这个问题的内容,但没有什么帮助。

安卓版本:4.0+
以下是我的代码:

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

我的网址是https://static.test.com/downloads/xxx.apk

虽然使用了https协议,但仍然出现错误:

02-10 11:18:23.710 E/AndroidRuntime( 7550): java.lang.RuntimeException: Unable to start service com.souyidai.investment.android.service.DownloadService@4344e5e0 with Intent { act=download_with_download_manager cmp=com.souyidai.investment.android/.service.DownloadService (has extras) }: java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs:  https://static.test.com/downloads/xxx.apk
02-10 11:18:23.710 E/AndroidRuntime( 7550):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2711)
02-10 11:18:23.710 E/AndroidRuntime( 7550):     at android.app.ActivityThread.access$2100(ActivityThread.java:141)
02-10 11:18:23.710 E/AndroidRuntime( 7550):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1299)
02-10 11:18:23.710 E/AndroidRuntime( 7550):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-10 11:18:23.710 E/AndroidRuntime( 7550):     at android.os.Looper.loop(Looper.java:136)
02-10 11:18:23.710 E/AndroidRuntime( 7550):     at android.app.ActivityThread.main(ActivityThread.java:5047)
02-10 11:18:23.710 E/AndroidRuntime( 7550):     at java.lang.reflect.Method.invokeNative(Native Method)
02-10 11:18:23.710 E/AndroidRuntime( 7550):     at java.lang.reflect.Method.invoke(Method.java:515)
02-10 11:18:23.710 E/AndroidRuntime( 7550):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
02-10 11:18:23.710 E/AndroidRuntime( 7550):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
02-10 11:18:23.710 E/AndroidRuntime( 7550):     at dalvik.system.NativeStart.main(Native Method)
02-10 11:18:23.710 E/AndroidRuntime( 7550): Caused by: java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs:  https://static.test.com/downloads/xxx.apk
02-10 11:18:23.710 E/AndroidRuntime( 7550):     at android.app.DownloadManager$Request.<init>(DownloadManager.java:642)
02-10 11:18:23.710 E/AndroidRuntime( 7550):     at com.souyidai.investment.android.service.DownloadService.onStartCommand(DownloadService.java:114)

以下是DownloadManager.Request代码:

    public Request(Uri uri) {
        if (uri == null) {
            throw new NullPointerException();
        }
        String scheme = uri.getScheme();
        if (scheme == null || (!scheme.equals("http") && !scheme.equals("https"))) {
            throw new IllegalArgumentException("Can only download HTTP/HTTPS URIs: " + uri);
        }
        mUri = uri;
    }

任何想法吗?
4个回答

8

我曾经遇到同样的问题。由于某种原因,在"https"之前我的url有一种不同的空格。在url字符串上使用trim()解决了这个问题。


我们有同样的问题。 - android_su
@EIYeante 在Galaxy S4上遇到了同样的问题,但是trim()并没有解决问题...请帮忙。 - Kostadin Georgiev

1

以下是帮助我解决问题的方法:

只需将您的URL中的 "\" 替换为 ""

像这样:

pdfUrl.replace("\", "").trim()

0
对我来说,这个错误是由于地址错误造成的。请检查您的URL。

-1

这是对我有效的方法:

addAndroidDownloads: {
          mediaScannable: true, // these two lines resolve the crash on first download
          mime: 'image/*', // define mime type also

你在哪里进行了配置? - cmptrwizard
@cmptrwizard 在 RN 应用程序中的包内调用函数配置 [不适用于 Android 原生] - Arush pande

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接