Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. notification.setProgress(0, 0, false);
  2. notification.setContentTitle("Complete");
  3. notification.setOngoing(false);
  4. notification.setPriority(NotificationCompat.PRIORITY_HIGH);
  5. notificationManager.notify(idd, notification.build());
  6. Log.e(TAG, "1");
  7.  
  8. private boolean saveFile(ResponseBody body, String fileName, String id) {
  9.  
  10. try {
  11. // todo change the file location/name according to your needs
  12. File futureStudioIconFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
  13.  
  14. InputStream inputStream = null;
  15. OutputStream outputStream = null;
  16.  
  17.  
  18. final int idd = Integer.parseInt(id);
  19.  
  20. Intent activityIntent = new Intent(this, MainActivity.class);
  21. PendingIntent contentIntent = PendingIntent.getActivity(this, 0, activityIntent, 0);
  22. final NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_2_ID)
  23. .setSmallIcon(R.drawable.ic_cloud_download_black_24dp)
  24. .setContentTitle("Download")
  25. .setContentText("Downloading in process")
  26. .setPriority(NotificationCompat.PRIORITY_HIGH)
  27. .setContentIntent(contentIntent);
  28.  
  29. notificationManager.notify(idd, notification.build());
  30.  
  31. try {
  32. byte[] fileReader = new byte[4096];
  33. long fileSize = body.contentLength();
  34. long fileSizeDownloaded = 0;
  35.  
  36. inputStream = body.byteStream();
  37. outputStream = new FileOutputStream(futureStudioIconFile);
  38.  
  39. while (true) {
  40. int read = inputStream.read(fileReader);
  41.  
  42. if (read == -1) {
  43.  
  44. break;
  45. }
  46.  
  47. outputStream.write(fileReader, 0, read);
  48.  
  49. fileSizeDownloaded += read;
  50.  
  51. Log.e(TAG, "file download: " + fileSizeDownloaded + " of " + fileSize);
  52. Long fileSizeD = fileSizeDownloaded;
  53. Long fileS = fileSize;
  54. int down = fileSizeD.intValue();
  55. int max = fileS.intValue();
  56. Log.e(TAG, "MAX: " + max + " Down: "+ down);
  57. notification.setProgress(max, down, false);
  58. notificationManager.notify(idd, notification.build());
  59. }
  60.  
  61. outputStream.flush();
  62.  
  63. notification.setProgress(0, 0, false);
  64. notification.setContentTitle("Downloasdfasad");
  65. notification.setOngoing(false);
  66. notification.setPriority(NotificationCompat.PRIORITY_HIGH);
  67. notificationManager.notify(idd, notification.build());
  68. Log.e(TAG, "1");
  69.  
  70. return true;
  71. } catch (IOException e) {
  72. return false;
  73. } finally {
  74. if (inputStream != null) {
  75. inputStream.close();
  76. }
  77.  
  78. if (outputStream != null) {
  79. outputStream.close();
  80.  
  81. }
  82. }
  83. } catch (IOException e) {
  84. return false;
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement