Guest User

Untitled

a guest
Jan 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. protected byte[] retrieveImageData() throws IOException {
  2. URL url = new URL(imageUrl);
  3. //HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  4. HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
  5.  
  6. // determine the image size and allocate a buffer
  7. int fileSize = connection.getContentLength();
  8. if (fileSize < 0) {
  9. return null;
  10. }
  11. byte[] imageData = new byte[fileSize];
  12.  
  13. // download the file
  14. Log.d(LOG_TAG, "fetching image " + imageUrl + " (" + fileSize + ")");
  15. BufferedInputStream istream = new BufferedInputStream(connection.getInputStream());
  16. int bytesRead = 0;
  17. int offset = 0;
  18. while (bytesRead != -1 && offset < fileSize) {
  19. bytesRead = istream.read(imageData, offset, fileSize - offset);
  20. offset += bytesRead;
  21. }
  22.  
  23. // clean up
  24. istream.close();
  25. connection.disconnect();
  26.  
  27. return imageData;
  28. }
Add Comment
Please, Sign In to add comment