This is my first time trying to implement DownloadManager and no matter what I try, I always get a notification saying "Download unsuccessful." I've looked at many other SO forums, a few tutorials, and what I have should work. Yes, I've set internet and external storage permissions in the manifest file. 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. 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. 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
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:
Dont't forget to add WRITE_EXTERNAL_STORAGE and INTERNET permissions in Manifest.
use requestPermissions() to grant permission from user.
use getExternalFilesDir() instead of getExternalStorageDirectory().
If you're downloading from http:// so add usesCleartextTraffic="true" to manifest.
Related
This is not working on Some Devices.
In Samsung Device they not allow to download file using Download manager.
I have already define permission in the manifest and also get RunTime Permission.
DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setTitle("");
request.setDescription("");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationUri(Uri.fromFile(imageFile));
request.setMimeType("*/*");
downloadManager.enqueue(request);
Use this
setDestinationInExternalFilesDir(
context.applicationContext,
Environment.DIRECTORY_DOWNLOADS,
""
)
I guess the problem with runtime permission if you are running the download manager on Marshamellow+, So you should request runtime permission, it's not adequate to add permission in Manifest file.
Check runtime permission from official documentation it would guide you to solve the problem.
Note: we should request storage permission as it considered as dangerous permission. on the other hand, we don't have to request Internet permission because it's a normal one. I hope this suffices.
I am using DownloadManager to download video file from internet to Environment.DIRECTORY_MOVIES. File is being downloaded, and when the download finishes I get ACTION_DOWNLOAD_COMPLETE broadcast.
But when I check download status there i get STATUS_FAILED back with REASON 406, which should be HTTP Error 406 Not acceptable. Also DownloadManager automatically deletes that file so I am not able to access it any more.
Strange thing is that it works on most devices and this is happening on Sony D5503 with android 5.1.1.
Do you have any idea why this can happen please?
From an Android application, I would like to use DownloadManager to download files from a peer which is connected using P2P (Wi-Fi Direct).
However, I found that unless the tablet/phone is connected to a WiFi network, DownloadManager will refuse to download over the P2P connection. It will output an error claiming "NO CONNECTION". If both Wi-Fi and P2P are connected, I can successfully download over the P2P connection.
Here's a sample code:
String url = "http://ipv4.download.thinkbroadband.com/5MB.zip";
String localFile = "5MB.zip";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setTitle("test.txt");
request.setDescription("Testing DownloadManager -- 5MB.zip");
request.setVisibleInDownloadsUi(true);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setDestinationInExternalFilesDir(context, null, localFile);
DownloadManager downloadManager = (DownloadManager)context.getSystemService(context.DOWNLOAD_SERVICE);
long downloadId = downloadManager.enqueue(request);
Now, I'm not sure if I'm just not using it correctly, or whether it's an unsupported feature. I'm willing to change Android's codebase if needed in order to get it to work (but I can't choose an alternative to DownloadManager).
Inspecting Android's code, it seems that the method checkCanUseNetwork() will return "NO_CONNECTION" because it gets a null NetworkInfo from mSystemFacade. See:
/packages/providers/DownloadProvider/src/com/android/providers/downloads/DownloadInfo.java
Further investigation hints to a "problem" in ConnectivityService. While it sets its mActiveDefaultNetwork to the correct type when Wi-Fi network is connected, it won't do anything when P2P is connected.
/frameworks/base/services/core/java/com/android/server/ConnectivityService.java
I'm using Nexus 4 with Android 5.0.1, but I've seen the same issue with KitKat 4.4.
If you remove following line of code, it will work.
"request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);"
Is it possible to dynamically request Android permission?
Android M introduces new run-time permission model which allows you to do this.
Yes,If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher.
code snippet:
if(ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
}
int result = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_PHONE_STATE);
if (result == 0)
// function which uses the permission
}
From targetapi>22 android need to have runtime permissions for performing some operations,Following is the code for requesting permission at run time in android
//checking wether the permission is already granted
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_CONTACTS) ==PackageManager.PERMISSION_GRANTED){
// permission is already granted
}else{
//persmission is not granted yet
//Asking for permission
ActivityCompat.requestPermissions(this,new String[] {
Manifest.permission.READ_CONTACTS},REQUEST_CODE);
}
The above code is for requesting read contact permissions. Similarly you can request other permissions also based on your requirement.
This solution address devices targeting API < 23, for 23 and above see Aun answer.
While many will answer this question with NO, and will tell you that it is a security risk, I found a way!
Take a look at ASTRO File Manager, one of the famouse file manager application for android. It can brows your SD-CARD, show your images, videos and the most interesting thing, it can install APKs!
So theoretically if I created an app that did not request for internet access during the install time, I could attach an APK inside my Raw library and install it in run time, and place all my permissions there. After that it's easy only a meter of applications communications. You can do this with broadcast or shared application id(read about it first it might be risky)
From user experience it will looks like you are asking a permission in run time.
I'm using the DownloadManager to queue up some downloads I'm making but running into this issue when I specifically try to use the Mobile/4G connection. I'm using an Android 2.3.4 phone. My code is using the 2.3.3 API.
I'm doing the following command (I want to force the connection to use 4G/3G)
request.setAllowedNetworkTypes(Request.NETWORK_MOBILE);
Whenever it attempts to download however, it places the download in the DownloadManager listing but it forever remains in the status "In progress" and an error at the top lists the file name and the error "Download requires network."
When I investigate further and connected my device to see the logs in logcat, I see the following error when it attempts to download:
Aborting request for download 92: download was requested to not use the current network type
I have the following permissions:
android.permission.WRITE_OWNER_DATA
android.permission.READ_OWNER_DATA
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.INTERNET
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_NETWORK_STATE
android.permission.CHANGE_WIFI_STATE
Any ideas of what it could be? Am I still missing a permission? Is there another setting that I need to control to specify the use of the Network connection only?
EDIT:
I have tried this on a brand new Galaxy tablet and this is the behavior I notice using this device: When the wifi is on and connected, it fails to download when specifying to use the NETWORK_MOBILE. If the wifi is turned off or not connected, it has no problem using the 4G connection. I'm thinking this is a security feature being done by the device, can this be overidden?
I dont know how you are setting up your download manager but this is how ive used it in the pass.
private long enqueue;
private DownloadManager dm;
public void onClick(View view) {
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse("File URL"));
enqueue = dm.enqueue(request);
}
public void showDownload(View view) {
Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);
}
This error occurs when you have no data connection (doesn't matter whether or not you are connected to wifi). You are allowed to specify NETWORK_MOBILE by itself, but it will throw you an error saying "Download Requires Network" if it tries to do the download when your data connection isn't currently working.
My advice to get a data connection again is to walk around until you get a connection again. I can tell when I have a data connection on my Droid Bionic phone because the top bar icons that say 4G LTE and the 4 bars turn blue instead of white.