简体   繁体   English

安卓下载管理器“下载不成功”

[英]Android Download Manager "Download Unsuccessful"

This is my first time trying to implement DownloadManager and no matter what I try, I always get a notification saying "Download unsuccessful."这是我第一次尝试实现DownloadManager ,无论我尝试什么,我总是收到一条通知,说“下载失败”。 I've looked at many other SO forums, a few tutorials, and what I have should work.我查看了许多其他 SO 论坛、一些教程以及我应该使用的内容。 Yes, I've set internet and external storage permissions in the manifest file.是的,我已经在清单文件中设置了 Internet 和外部存储权限。 And yes, I've given storage permission in the app settings on the phone.是的,我已经在手机的应用程序设置中授予了存储权限。 I've tried this on both an Android emulator running API 28 and a real phone running the same.我已经在运行 API 28 的 Android 模拟器和运行相同的真实手机上尝试过这个。 Here is the code I have:这是我的代码:

        String url = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4";

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

        request.setTitle("title");

        request.setDescription("Your file is downloading");

        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, "" + System.currentTimeMillis());

        request.allowScanningByMediaScanner();
        request.setAllowedOverMetered(true);
        request.setAllowedOverRoaming(true);

        //Enqueue download and save the referenceId
        long downloadReference = downloadManager.enqueue(request);

        if (downloadReference != 0) {
            Toast.makeText(getApplicationContext(), "download started", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(getApplicationContext(), "no download started", Toast.LENGTH_SHORT).show();
        }

Any help or suggestions are appreciated.任何帮助或建议表示赞赏。 Thanks.谢谢。

This issue occur due to network security.出现此问题是由于网络安全。 If You are using un-secure url in above pie API, then it can't execute your url.如果您在上面的 pie API 中使用不安全的 url,则它无法执行您的 url。 Check Official Documentation .检查官方文档

Reason for avoiding cleartext traffic is the lack of confidentiality, authenticity, and protections against tampering;避免明文流量的原因是缺乏机密性、真实性和防篡改保护; a network attacker can eavesdrop on transmitted data and also modify it without being detected.网络攻击者可以窃听传输的数据,并在不被发现的情况下对其进行修改。

Add following in manifest to bypass all security.在清单中添加以下内容以绕过所有安全性。

<application
  android:name=".ApplicationClass"
  ....
  android:usesCleartextTraffic="true">

My Experience on 1/11/2021 , min SDK 19, Target SDK 30我在1/11/2021上的体验,最低 SDK 19,目标 SDK 30

I spent a day on using Download Service and finally it worked.我花了一天时间使用下载服务,终于成功了。 To sum it up for anyone who wants to try for the first time:给想第一次尝试的人总结一下:

  1. Dont't forget to add WRITE_EXTERNAL_STORAGE and INTERNET permissions in Manifest.不要忘记在 Manifest 中添加 WRITE_EXTERNAL_STORAGE 和 INTERNET 权限。
  2. use requestPermissions() to grant permission from user.使用 requestPermissions() 授予用户权限。
  3. use getExternalFilesDir() instead of getExternalStorageDirectory().使用 getExternalFilesDir() 而不是 getExternalStorageDirectory()。
  4. If you're downloading from http:// so add usesCleartextTraffic="true" to manifest.如果您从 http:// 下载,则添加 usesCleartextTraffic="true" 到清单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM