I'm not exactly sure what my problem is. I using the code below to download a bunch of files:
public void DownloadNow(ArrayList<String> must_download)
{
String serviceString = Context.DOWNLOAD_SERVICE;
DownloadManager download;
download = (DownloadManager)getSystemService(serviceString);
for(int x = 0; x < must_download.size(); x++)
{
System.out.println("DOWNLOADING "+must_download.get(x).toString());
Uri uri = Uri.parse("http://drm.csdev.nmmu.ac.za/Zandre/"+must_download.get(x).toString());
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, must_download.get(x).toString());
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
request.setTitle(must_download.get(x).toString());
long reference = download.enqueue(request);
}
}
Where must_download is an ArrayList full of file names. This method has successfully worked in the past, until I tried to run it on a different thread. The thread idea did not work out so well, so I decided to switch back.
Now, the problem is this- since I switched back to the original method the Download Manager runs full time, downloading the same files over and over in an endless loop.The only way I can stop it is to **Disable the Download Manager, but then I can't download anything...
What I've read so far and tried is
Uninstall the app- still downloading...
Switch device off and back on again- still downloading...
Clear cache of Download Manager- still downloading...
Even after this has used all the devices mobile data, it still just carries on downloading.
Has anyone else encountered this type of problem? Any ideas solutions?
Related
I am trying to download a file (size - 4 MB, extenssion - .apk). When download completes, I am giving it to Android's install manager to do it's job. Now my problem is, if I download the file multiple times (4 -5 times), my phone gets restarted.
UI Flow
I have a button with "Download" as text, on click of it, I execute the below code:
static void downloadApp(Activity context, StorageReference storageRef, String fileName) throws IOException {
File localFile = new File(Environment.getExternalStorageDirectory() + File.separator + fileName);
localFile.createNewFile();
storageRef.child(fileName)
.getFile(localFile)
.addOnSuccessListener(taskSnapshot -> installApp(context, localFile))
.addOnFailureListener(Crashlytics::logException);
}
And on success of the download, I am trying to install the app as below:
private static void installApp(Activity context, File apkFile) {
Intent installIntent = new Intent(Intent.ACTION_VIEW);
installIntent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
context.startActivity(installIntent);
}
When the install manager opens, I click "Cancel" button to go back and click on "Download" button again.
If I repeat the step for 4 times (sometimes 5), my phone reboots.
Please let me know where I am doing wrong. I am guessing it as a defect from Firebase SDK, as I have followed the Documentation guide carefully here. And even though something wrong is there in my code, it should throw any Exception or in worst case an Error (maybe OutOfMemory), but instead it restarts. (A very bad experience for my users)
There is something I don't understand about Android DownloadManager because if I download a file from Chrome (it's the same when I download a file from my own application) and while the file is being downloaded, if I take a look in sdcard/Download with a file explorer, I can see the file named with it's final name and with it's final size whereas the file is not available again.
So, I wonder how to know when the file is ok or not.
Oh sure, if I'm still running my application, I can just wait to be notified by he Broadcast receiver but that's a situation which could not occur for long time downloads.
If I leave my app and run it a moment after, how to check my downloads?
With Curser you can find status of your file. Your code maybe something like this :
mIdColumnId = cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);
mTitleColumnId = cursor
.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE);
mStatusColumnId = cursor
.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS);
mReasonColumnId = cursor
.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON);
mTotalBytesColumnId = cursor
.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
mCurrentBytesColumnId = cursor
.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
mMediaTypeColumnId = cursor
.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE);
mDateColumnId = cursor
.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP);
By dividing TOTAL_SIZE and DOWNLOAD_SO_FAR you can find what percent of file downloaded.
for complete code take a look this library.
I am noticing some strange behaviour while using DownloadManager class to download videos from my server.
The videos are downloaded just fine, but when I try to delete them using the public int remove (long... ids) function, the physical file doesn't get deleted and memory ultimately isn't released either.
Here's how i am giving the DownloadManager.Request object to the DownloadManager's enqueue function:
Uri target = Uri.fromFile(new File(destinationFolder, Sha1Util.SHA1(url)));
Request request = new Request(Uri.parse(url));
request.setDestinationUri(target);
request.setNotificationVisibility(Request.VISIBILITY_HIDDEN);
request.setVisibleInDownloadsUi(false);
where destinationFolder = /storage/emulated/0/Android/data/{app's_package_name}/{user_name}
NOTE: The remove function is working fine on Lollipop devices but isn't working on prior versions.
My app has 5 big text files on them, each one about 40kb. Those files are updated weekly on a website.
I managed to set downloadManager to download the 5 files from the site. The problem is that I don't want the user to be able to use the app while the files are downloading because that will cause problems.
I want that when the user clicks the "Update" button it will show up a loading box. Then when the files finish downloading the box will disappear and a toast will say "Updated."
This is the method I've set up:
#SuppressLint("NewApi")
public void DownloadTxtFile(String url, String fileName) {
if (!new File(Environment.getExternalStorageDirectory()
+ "/folder").exists()) {
new File(Environment.getExternalStorageDirectory()
+ "/Dovahzul_Dictionary").mkdirs();
}
File file = new File(extStore.getAbsolutePath()
+ "/folder/" + fileName);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.setDescription("Downloading Text Files...");
request.setTitle("Updating")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir("/Dovahzul_Dictionary",
fileName);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
if (file.exists()) {
file.delete();
} else {
}
manager.enqueue(request);
}
By the way, if you find any problem with the method, plase inform me as well.
Thanks in advance.
I know this is old, but to anyone that has the same problem: What you need to do, is start the download in a different thread.
As android is not thread prof, you need to use AsyncTask instead. An AsyncTask is very similar to a thread. By starting and using one, you can do some quite long operations without freezing the ui. (Note that AsyncTasks are recomended for operations that take seconds, not minutes. In that case, you need to find something else)
You start an AsyncTask, and make it download the file. The problem is that AsyncTasks can't access ui methods, so they can't change threads.
You can, luckly, override a method they have to update the ui, and call it directly from it's onExecute.
Many tutorials on this can be found in the web.
I am attempting a download through the DownloadManager. It is calling just fine and seems to initialize just fine. I can see a file is created in my "music" directory and it even starts to grow a bit. Once it gets to around 3-4 MB then the download will cancel and say "download" unsuccessful. This might be because I am downloading from a slow source but it is definitely available. Any ideas on how to not have this quit/timeout. Below is the function that i am using.
In case it is important. I am calling this from an Asynchronous method and I am looping thorough a map that will begin the download of 20 (or so) mp3 files.
Thanks,
Craig
private void download(String strUrl, String fileName){
try {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(strUrl));
request.setDescription(strUrl);
request.setTitle(fileName);request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC,fileName);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
} catch (Exception e) {
e.printStackTrace();
Log.i("ERROR: ", e.getCause().getMessage());
}
}
FWIW - I never found a good answer to this question but once I fixed my network it seemed to go away. It still occassionally happens but it is relatively infrequent and appears to be network related.