public void StartDownloading() { File file = null; for(int i=0; i<FileNames.length; i++) { file = new File("/sdcard/Sounds/"+FileNames[i]); boolean exists = file.exists(); if(exists) { continue; } else { new DownloadFileAsync().execute(Path+FileNames[i],FileNames[i]); } file = null; } } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_DOWNLOAD_PROGRESS: WaitDialog = new ProgressDialog(this); WaitDialog.setMessage("Downloading files..."); WaitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); WaitDialog.setCancelable(false); WaitDialog.show(); return WaitDialog; default: return null; } } class DownloadFileAsync extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); showDialog(DIALOG_DOWNLOAD_PROGRESS); } @Override protected String doInBackground(String... aurl) { int count; try { URL url = new URL(aurl[0]); URLConnection conexion = url.openConnection(); conexion.connect(); int lenghtOfFile = conexion.getContentLength(); InputStream input = new BufferedInputStream(url.openStream()); File file = new File("/sdcard/Sounds/"+aurl[1]); OutputStream output = new FileOutputStream(file); byte data[] = new byte[4046848]; long total = 0; while ((count = input.read(data)) != -1) { total += count; publishProgress(""+(int)((total*100)/lenghtOfFile)); output.write(data, 0, count); } output.flush(); output.close(); input.close(); } catch (Exception e) {} return null; } @Override protected void onPostExecute(String unused) { dismissDialog(DIALOG_DOWNLOAD_PROGRESS); } } 

At the same time, I try to download 4 files with a total weight of 2.71 mb, the progress bar appears, but usually disappears quickly, and the files remain downloaded at 5-30kb, sometimes even 1-2 files appear, too, at 5-30kb.

  • I suspect that an exception is thrown in doInBackground (), which is successfully caught by catch. Try to make an exception in the log in the catch block and view it later. - Sergeich
  • There was an error Out of memory on a 10046864-byte allocation. reduced. The effect is the same, except for errors. - AndroidDev
  • Is there permission to use the INTERNET? - Dex
  • If it even loads small pieces. Love is - AndroidDev
  • one
    Why do you even make such a giant buffer? There is no point in this: you will never read more than the BufferedInputStream buffer size (by default it is 8192). In addition, it makes no sense at all to make it so gigantic: 4096 or 8192 is enough when reading from the network. It makes no more sense at all, it is a waste of resources. - cy6erGn0m

1 answer 1

To catch the completion of all downloads, I would write something like this

 List<File> filesToBeDownloaded = getNonExistFiles(); CountDownLatch latch = new CountDownLatch(filesToBeDownloaded.size()); for(File f : filesToBeDownloaded) { new FileDownloadTask().execute(f.getName(), latch); } new DlAwaitTask().execute(latch); /// ... class FileDownloadTask extends AsyncTask<Void, String, CountDownLatch> { // ... @Override protected Void doInBackground(String fileName, CountDownLatch latch) { try { // perform download } ... } finally { latch.countDown(); } } } } class DLAwaitTask exetends AsyncTask<Void, CountDownLatch> { //..... @Override protected Void doInBackground(String fileName, CountDownLatch latch) { try { if(latch.await(MY_DEFAULT_TIMEOUT, TimeUnit.SECONDS)) // download complete.. notify UI else // timeout.. show error, etc } .... } finally { // destroy all anyaway } } 

Of course, this is a template where you can enter your code. In addition, it does not interfere with digging into the android API. Perhaps there is a way to run these tasks through a common pool of threads and wait for all tasks to complete in some more freeware way.

  • Unresolved with part 2 - AndroidDev
  • With DLAwaitTask? We run this task. It waits for the latch until it works (this will happen when the counter goes to zero). Until it reaches zero, the stream will be blocked in latch.await and will sleep. It will be removed from the lock, if the lot is reset, a timeout or interrupt occurs. Each download task ends with latch.countDown, thereby reporting that there is one less working task. - cy6erGn0m
  • The mud is too confused, we just need to determine when all the files are uploaded and remove the progresbar from the screen - AndroidDev
  • Only? You have N parallel threads. They need to somehow synchronize. You have almost no options. Unless to start downloads in turn, and not all at once. Besides, where did you see something confused here? Here a completely tiny task was added and literally three lines were added in the rest of the code. Here is creating a buffer for a simple network operation of 4 megabytes in size - this is where the confusion is;) - cy6erGn0m
  • Hmm ok I tried, there are problems ... Incorrect number of arguments for type AsyncTask <Params, Progress, Result>; it cannot be parameterized with arguments <Void, CountDownLatch> And about this List <File> filesToBeDownloaded = getNonExistFiles (); CountDownLatch latch = new CountDownLatch (filesToBeDownloaded.size ()); I wonder how to get them? Maybe just pre-specify the size known to me? - AndroidDev