3

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:

java.net.URL read stream to byte[]

Efficiently read file from URL into byte[] in Java

Get image with given url and convert it to byte array

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:

private class SetProfPicWithURL extends AsyncTask<URL, Integer, byte[]> {
        @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;
        }
    }

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:

The file “tfss-0280f98d-7180-4528-9d24-3ec47d3b25d4-image.jpg” could not be opened because it is empty.

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!

Community
  • 1
  • 1
ThePartyTurtle
  • 2,276
  • 2
  • 18
  • 32

1 Answers1

3

Directly save your imagefile as profile picture like this :

final ParseFile imageFile = new ParseFile("image.jpg", bytes);
ParseUser.getCurrentUser().put(ParseUtils.PARSE_PROFILE_IMAGE, imageFile);
ParseUser.getCurrentUser().saveInBackground(new SaveCallback() {
    @Override
    public void done(ParseException e) {
        if (e == null) {
            Log.i("LoginActivity", "Profile saved succesfully");

        } else {
            e.printStackTrace();
        }
    }
});

EDIT :

Use this to get image byte array from url.

try {
    java.net.URL img_value = new java.net.URL(imageURL);        
    Bitmap mIcon = BitmapFactory
            .decodeStream(img_value.openConnection()
                    .getInputStream());
    if (mIcon != null)
        imgByteArray = encodeToByteArray(mIcon);
} catch (Exception e) {
    e.printStackTrace();
}


public byte[] encodeToByteArray(Bitmap image) {
    Log.d(TAG, "encodeToByteArray");
    Bitmap b= image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    b.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imgByteArray = baos.toByteArray();

    return imgByteArray ;
}
Ved
  • 1,035
  • 14
  • 28
  • This is correct, but also not my issue. My issue is that I need to download the bytes from a URL, then save the byte's to a ParseFile. I use (essentially) the code that you wrote in my snippet above. The problem is that when the ParseFile is saved, the file is empty, which I suspect has something to do with how I download the bytes. – ThePartyTurtle Feb 09 '16 at 20:34
  • I'm gonna try this now! – ThePartyTurtle Feb 11 '16 at 17:02
  • Just checked it out. I am receiving this error: "D/skia: --- SkImageDecoder::Factory returned null" I know that the URL is correct, because when I print the url to the console, it brings up the correct profile picture. Any idea why this error would be thrown? – ThePartyTurtle Feb 11 '16 at 17:56