title: Image URL to ParseFile?
body: <p>I have a Parse Android app for which I am implementing Facebook sign up. Currently I am stuck on grabbing images to set as profile pictures of new ParseUser's. I have successfully used the Facebook Graph API to retrieve the correct URL (I have checked this by plugging it into a browser, where I am shown the right profile picture), but I now need a way to turn that URL into a byte array (byte[]) so that I can save the ParseFile field of our ParseUser's profile picture. I have already looked at all these SO questions:</p>
  
  <p>• <a href="https://stackoverflow.com/questions/2295221/java-net-url-read-stream-to-byte">java.net.URL read stream to byte[]</a></p>
  
  <p>• <a href="https://stackoverflow.com/questions/27001824/efficiently-read-file-from-url-into-byte-in-java">Efficiently read file from URL into byte[] in Java</a></p>
  
  <p>• <a href="https://stackoverflow.com/questions/19467386/get-image-with-given-url-and-convert-it-to-byte-array">Get image with given url and convert it to byte array</a></p>
  
  <p>None of these have worked. I am currently trying to use the Apache IOutils, like in the solution from the second link. Here is my current code for the AsyncTask:</p>
  
  <pre><code>private class SetProfPicWithURL extends AsyncTask&lt;URL, Integer, byte[]&gt; {
          @Override
          protected byte[] doInBackground(URL... imageURL) {
              Log.i("SetProfPicWithURL", "invocation, URL: " + imageURL[0]);
              InputStream is = null;
              byte[] bytes = null;
              try {
                  is = imageURL[0].openStream();
                  bytes = IOUtils.toByteArray(is);
              } catch (IOException e) {
                  e.printStackTrace();
              }
              finally {
                  if (is != null) try {
                      is.close();
  
                      if(bytes == null){Log.e("LoginActivity", "bytes is null int SetProfPicWithURL");}
                      final ParseFile imageFile = new ParseFile("image.jpg", bytes);
                      imageFile.saveInBackground(new SaveCallback() {
                          @Override
                          public void done(ParseException e) {
                              if (e == null) {
                                  Log.i("LoginActivity", "getCurrentUser.put");
                                  ParseUser.getCurrentUser().put(ParseUtils.PARSE_PROFILE_IMAGE, imageFile);
                                  ParseUser.getCurrentUser().saveInBackground();
                              } else {
                                  e.printStackTrace();
                              }
                          }
                      });
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
              return bytes;
          }
      }
  </code></pre>
  
  <p>Now when this code executes, I get no error logs, and a ParseFile is created. However, no profile pictures load within the app, and when I click to examine the file in the dashboard, I get this error message:</p>
  
  <blockquote>
    <p>The file “tfss-0280f98d-7180-4528-9d24-3ec47d3b25d4-image.jpg” could
    not be opened because it is empty.</p>
  </blockquote>
  
  <p>Honestly, I'm at a loss. I've spent significantly more time on this one photo issue than any other part of implementing the Facebook login. And the way our database is set up, it is really not ideal to create another field to save the URL and load with Picasso. Any help with this issue is truly appreciated!</p>