2

I've been stuck at this whole day. I've tried all other means available to accomplish this. This below approach almost did the work but it's giving a null pointer exception for the variable file.

The code below describes about saving data

            final ParseObject post = new ParseObject("Student");
            post.put("name", nameS);

            final ParseFile file = new ParseFile("photo.png", data);
            file.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    post.put("studentPhoto", file);
                    Log.d("John", ""+ file);
                }
            });

            post.put("author", ParseUser.getCurrentUser());

            post.saveInBackground(new SaveCallback() {
                public void done(ParseException e) {
                    if (e == null) {
                        // Saved successfully.
                        Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(AddStudentActivityEditText.this, AddStudentActivityTextView.class);
                        startActivity(intent);

                    } else {
                        // The save failed.
                        Toast.makeText(getApplicationContext(), "Failed to Save", Toast.LENGTH_SHORT).show();
                        Log.d(getClass().getSimpleName(), "User update error: " + e);
                    }
                }
            });

Below data describes about Retrieving data

final ParseQuery<ParseObject> query = ParseQuery.getQuery("Student");
    query.whereEqualTo("author", ParseUser.getCurrentUser());
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {

                for (ParseObject text : objects) {

                    name.setText(text.getString("name"));
                 }

                ParseQuery<ParseObject> query = ParseQuery.getQuery("Student");
                query.whereEqualTo("author", ParseUser.getCurrentUser());
                query.getFirstInBackground(new GetCallback<ParseObject>() {
                    public void done(ParseObject object, ParseException e) {
                        if (object != null) {

                            ParseFile file = (ParseFile)object.get("studentPhoto");
                            file.getDataInBackground(new GetDataCallback() {


                                public void done(byte[] data, ParseException e) {
                                    if (e == null) {

                                        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                                        //use this bitmap as you want
                                        photo.setImageBitmap(bitmap);

                                    } else {
                                        // something went wrong
                                    }
                                }
                            });

                        } else {
                            Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_SHORT) .show();

                        }
                    }
                });

            } else {
                Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_SHORT).show();

            }
        }
    });

1 Answer 1

1

I don't know why your approach didn't working, but I will show you my approach for getting photos from parse. First, make sure that you successfully saved your image - to do that, go to your app on parse.com panel, open Core tab, and check in your Student table if there is a column named studentPhoto, and there should be links to that photos. And my approach for getting image from parse is to use Picasso library. Simply, from parse you should get only link to photo, not whole file, and then download that photo using Picasso. To get link to file do something like that:

ParseObject object = ... // your student object
try {
  object.fetchIfNeeded();
} catch (ParseException e) {
  e.printStackTrace();
}

ParseFile avatar  = object.getParseFile("studentPhoto");
String avatarUrl = avatar.getUrl();

And then set that image using Picasso:

   Picasso.with(getActivity()) // instead of getActivity() you could pass there any context
                    .load(avatarUrl)
                    .into(yourImageViewWithAvatar);

There is Picasso website for more info: http://square.github.io/picasso/

Sign up to request clarification or add additional context in comments.

3 Comments

there is student column , but there isn't any link there, it's got some stuff and it's type is in byte
Hmm, there should be a student table, not column. And in that table should be a column named studentPhoto.
yeah, exactly....but there isn't any link, and it's type is byte...I'll try your approach sometime later and let you know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.