Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use AsyncTask to download files?
    primarykey
    data
    text
    <p>I'm Using this class to download files:</p> <pre><code>public class DownloadService extends Service { String downloadUrl; LocalBroadcastManager mLocalBroadcastManager; ProgressBar progressBar; File sdCard = Environment.getExternalStorageDirectory(); File dir = new File (sdCard.getAbsolutePath() + "/org.test.download/"); double fileSize = 0; DownloadAsyncTask dat; @Override public IBinder onBind(Intent arg0) { return null; } public DownloadService(String url,Context c, ProgressBar pBar){ downloadUrl = url; mLocalBroadcastManager = LocalBroadcastManager.getInstance(c); progressBar = pBar; dat = new DownloadAsyncTask(); dat.execute(new String[]{downloadUrl}); } private boolean checkDirs(){ if(!dir.exists()){ return dir.mkdirs(); } return true; } public void cancel(){ dat.cancel(true); } public class DownloadAsyncTask extends AsyncTask&lt;String, Integer, String&gt;{ @Override protected String doInBackground(String... params) { String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/")+1); if(!checkDirs()){ return "Making directories failed!"; } try { URL url = new URL(downloadUrl); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); urlConnection.connect(); fileSize = urlConnection.getContentLength(); FileOutputStream fos = new FileOutputStream(new File(dir,fileName)); InputStream inputStream = urlConnection.getInputStream(); byte[] buffer = new byte[500]; int bufferLength = 0; int percentage = 0; double downloadedSize = 0; while ( (bufferLength = inputStream.read(buffer)) &gt; 0 ) { if(isCancelled()){ break; } fos.write(buffer, 0, bufferLength); downloadedSize += bufferLength; percentage = (int) ((downloadedSize / fileSize) * 100); publishProgress(percentage); } fos.close(); urlConnection.disconnect(); } catch (Exception e) { Log.e("Download Failed",e.getMessage()); } if(isCancelled()){ return "Download cancelled!"; } return "Download complete"; } @Override protected void onProgressUpdate(Integer... values){ super.onProgressUpdate(values[0]); if(progressBar != null){ progressBar.setProgress(values[0]); }else{ Log.w("status", "ProgressBar is null, please supply one!"); } } @Override protected void onPreExecute(){ mLocalBroadcastManager.sendBroadcast(new Intent("org.test.download.DOWNLOAD_STARTED")); } @Override protected void onPostExecute(String str){ mLocalBroadcastManager.sendBroadcast(new Intent("org.test.download.DOWNLOAD_FINISHED")); } @Override protected void onCancelled(){ mLocalBroadcastManager.sendBroadcast(new Intent("org.test.download.DOWNLOAD_CANCELLED")); } } } </code></pre> <p>I'm using this because apparently <code>DownloadManager</code> wont work prior to <code>API 9</code> and i'm targeting <code>API 7</code></p> <p>I have <code>ListView</code> which parses a XML File and shows packages that can be downloaded.</p> <p>How can I modify this class to accept Array of strings containing URLs and download them one by one ?</p> <p>Or is there any good way to download List of files ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload