I am trying to download a file from the internet with DownloadManager on Android 9, but I only get unsuccessful download. This is what I am testing:
My Manifest permission:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
My code to download it:
private final String FILE_NAME = "SampleDownloadApp.apk";
String apkUrl = "https://androidwave.com/source/apk/app-pagination-recyclerview.apk";
public void download(Context context) {
DownloadManager dm = (DownloadManager)context.getSystemService(DOWNLOAD_SERVICE);
Uri mUri = Uri.parse(apkUrl);
DownloadManager.Request r = new DownloadManager.Request(mUri);
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Uri uri = Uri.parse("file://" + context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + FILE_NAME_2);
r.setDestinationUri(uri);
dm.enqueue(r);
}
I have tried android: usesCleartextTraffic = "true" in the manifest but then nothing happens. I never get any errors neither in logcat nor in run.
I followed this tutorial: https://androidwave.com/download-and-install-apk-programmatically/
I have no idea what the problem may be, any solution?
Help with this!
there is something wrong with you URL tried following and its working
private final String FILE_NAME = "SampleDownloadApp.apk";
String apkUrl = "https://d-10.winudf.com/b/APK/Y29tLm5lbW8udmlkbWF0ZV80MzUxMV9iOTJjMTFkZA?_fn=VmlkTWF0ZSBIRCBWaWRlbyBEb3dubG9hZGVyIExpdmUgVFZfdjQuMzUxMV9hcGtwdXJlLmNvbS5hcGs&_p=Y29tLm5lbW8udmlkbWF0ZQ&am=cCsiKSeBEin6IVuzn-Q2PA&at=1590661203&k=3c1a8eca32edf42751cb3f5fa102d0205ed0e1d3";
public void download(Context context) {
DownloadManager dm = (DownloadManager)context.getSystemService(DOWNLOAD_SERVICE);
Uri mUri = Uri.parse(apkUrl);
DownloadManager.Request r = new DownloadManager.Request(mUri);
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, FILE_NAME);
dm.enqueue(r);
}