Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can customize and use this, you will need the wakelock permission to prevent interruption </p> <pre><code>@SuppressLint("Wakelock") public class DownloadTask extends AsyncTask&lt;String, Integer, String&gt; { private Context context; public DownloadTask(Context context ,Dialog dialog, ProgressBar progressBar ,TextView progressTextView , String destinationPath ,String fileName , JSONObject jObject ) { this.context = context; } @SuppressWarnings("resource") @Override protected String doInBackground(String... sUrl) { String directory = sUrl[0]; String fileName = sUrl[1]; //prevent CPU from going off if the user presses the power button during download PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName()); wl.acquire(); //download try { new File(directory).mkdirs(); InputStream input = null; OutputStream output = null; HttpURLConnection connection = null; try { //connect to url URL url = new URL(sUrl[2]); connection = (HttpURLConnection) url.openConnection(); connection.connect(); // check for http_ok (200) if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) return "Server returned HTTP " + connection.getResponseCode() + " " + connection.getResponseMessage(); int fileLength = connection.getContentLength(); // download the file input = connection.getInputStream(); output = new FileOutputStream(directory+"/"+fileName+".mp3");//change extension //copying byte data[] = new byte[4096]; long total = 0; int count; while ((count = input.read(data)) != -1) { // allow canceling if (isCancelled()) { new File(directory+"/"+fileName+".mp3").delete();//delete partially downloaded file return null; } total += count; if (fileLength &gt; 0 ) //publish progress only if total length is known publishProgress( (int)(total/1024) , fileLength/1024 );//(int) (total * 100 / fileLength)); output.write(data, 0, count); } } catch (Exception e) { return e.toString(); } finally //closing streams and connection { try { if (output != null) output.close(); if (input != null) input.close(); } catch (IOException ignored) { } if (connection != null) connection.disconnect(); } } finally { wl.release(); // release the lock screen } return null; } @Override // onPreExecute and onProgressUpdate run on ui thread so you can update ui from here protected void onPreExecute() { super.onPreExecute(); } @Override protected void onProgressUpdate(Integer... progress) { super.onProgressUpdate(progress); } @Override protected void onPostExecute(String result) { if (result != null) Toast.makeText(context, "Download error: " + result,Toast.LENGTH_SHORT).show(); else { Toast.makeText(context, " download complete ",Toast.LENGTH_SHORT).show(); } } } </code></pre> <p>and then call it from main</p> <pre><code>new DownloadTask(this).execute( dirPath , fileName , urlToDownload ); </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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