Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class DownloadFileFromURL extends AsyncTask<String, String, String> {
  2.  
  3. public String url;
  4. public String pathLoc;
  5. public String imgName;
  6. private OnPreExecute mPreExecute;
  7. private OnProgressUpdated mProgress;
  8. private OnResultCompleted mProgCompleted;
  9.  
  10. public DownloadFileFromURL(String url, String pathLoc, String imgName, OnPreExecute preExecute, OnProgressUpdated progressUpdated, OnResultCompleted progressCompleted) {
  11. this.setUrl(url);
  12. this.setPathLoc(pathLoc);
  13. this.setImgName(imgName);
  14. this.mPreExecute = preExecute;
  15. this.mProgress = progressUpdated;
  16. this.mProgCompleted = progressCompleted;
  17.  
  18. }
  19.  
  20. protected void onPreExecute() {
  21. super.onPreExecute();
  22. if (mPreExecute != null) mPreExecute.onPreExecute();
  23. }
  24.  
  25. @Override
  26. protected String doInBackground(String... params) {
  27. int count;
  28. try {
  29. URL url = new URL(getUrl());
  30. URLConnection conection = url.openConnection();
  31. conection.connect();
  32. OutputStream output = null;
  33.  
  34. // this will be useful so that you can show a tipical 0-100% progress bar
  35. int lenghtOfFile = conection.getContentLength();
  36.  
  37. // download the file
  38. InputStream input = new BufferedInputStream(url.openStream(), 819200);
  39.  
  40. String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + getPathLoc();
  41. File file = new File(path);
  42. if (!file.exists()) {
  43. file.mkdir();
  44. }
  45.  
  46. // Output stream
  47. output = new FileOutputStream(path + "/" + getImgName());
  48.  
  49. byte data[] = new byte[1024];
  50.  
  51. long total = 0;
  52.  
  53. while ((count = input.read(data)) != -1) {
  54. total += count;
  55. // publishing the progress....
  56. // After this onProgressUpdate will be called
  57. publishProgress("" + (int) ((total * 100) / lenghtOfFile));
  58.  
  59. // writing data to file
  60. output.write(data, 0, count);
  61. }
  62.  
  63. // flushing output
  64. output.flush();
  65.  
  66. // closing streams
  67. output.close();
  68. input.close();
  69.  
  70. } catch (Exception e) {
  71. Log.e("Error: ", e.getMessage());
  72. }
  73.  
  74. return null;
  75. }
  76.  
  77. @Override
  78. protected void onPostExecute(String result) {
  79. if (mProgCompleted != null) mProgCompleted.onResult(result);
  80. }
  81.  
  82. protected void onProgressUpdate(String... progress) {
  83. // setting progress percentage
  84. if (mProgress != null) mProgress.onProgress(progress[0]);
  85. }
  86.  
  87. public interface OnPreExecute {
  88. public void onPreExecute();
  89. }
  90.  
  91. public interface OnProgressUpdated {
  92. public void onProgress(String... progress);
  93. }
  94.  
  95. public interface OnResultCompleted {
  96. public void onResult(String result);
  97. }
  98.  
  99.  
  100. /* Setter Getter*/
  101. public String getUrl() {
  102. return url;
  103. }
  104.  
  105. public void setUrl(String url) {
  106. this.url = url;
  107. }
  108.  
  109. public String getPathLoc() {
  110. return pathLoc;
  111. }
  112.  
  113. public void setPathLoc(String pathLoc) {
  114. this.pathLoc = pathLoc;
  115. }
  116.  
  117. public String getImgName() {
  118. return imgName;
  119. }
  120.  
  121. public void setImgName(String imgName) {
  122. this.imgName = imgName;
  123. }
  124. }