ThePartyTurtle
ThePartyTurtle

Reputation: 2606

Image URL to ParseFile?

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!

Upvotes: 3

Views: 1323

Answers (1)

Ved
Ved

Reputation: 1077

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 ;
}

Upvotes: 3

Related Questions