Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. @Override
  2. protected Void doInBackground(String... params) {
  3. InputStream inputStream = null;
  4. FileOutputStream fileOutput = null;
  5. try {
  6. URL url = new URL(params[0]);
  7. File file = new File(path);
  8. URLConnection urlConnection = url.openConnection();
  9.  
  10. inputStream = urlConnection.getInputStream();
  11. fileOutput = new FileOutputStream(file);
  12.  
  13. int totalSize = urlConnection.getContentLength();
  14. int downloadedSize = 0;
  15.  
  16. byte[] buffer = new byte[16384];
  17. int bufferLength = 0;
  18.  
  19. while ((bufferLength = inputStream.read(buffer)) > 0 ) {
  20.  
  21. while(isPaused) {
  22. sleep();
  23. }
  24.  
  25. if(isCancelled()) {
  26. if(file.exists())
  27. file.delete();
  28. return null;
  29. }
  30.  
  31. fileOutput.write(buffer, 0, bufferLength);
  32. downloadedSize += bufferLength;
  33. publishProgress(downloadedSize, totalSize);
  34. }
  35.  
  36. if(totalSize > getFreeMemorySize()) {
  37. //if(true) {
  38. if(errorHandler != null)
  39. errorHandler.onMemorySizeException();
  40. cancel(true);
  41. }
  42. } catch (IOException e) {
  43. int i = e.hashCode();
  44. e.getStackTrace();
  45. }
  46. finally {
  47. try {
  48.  
  49. if(inputStream != null)
  50. inputStream.close();
  51. if(fileOutput != null)
  52. fileOutput.close();
  53.  
  54. } catch (IOException e) {
  55. int i = e.hashCode();
  56. e.getStackTrace();
  57. }
  58. }
  59. return null;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement