user3827788
user3827788

Reputation:

Saving images to Parse

I am trying to have the users upload their image (profile picture) into parse as part of their profile creation page where they would have to fill out various information. Users would be able to preview their picture in an image view.

I have tried to achieve this by taking the following steps.

1) Creating a button that would allow users to retrieve a picture from their gallery, and to upload it into the ImageView created.

Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                    && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            }

        }
        } 

This part has generally been executed with success with users being able to upload image from their gallery and seeing it visually displayed in the image view.

2) Storing user uploaded images on parse This is the area where I am struggling at. Comments have been added in between the code for further clarification, and to highlight were my questions are.

 ParseUser currentUser = ParseUser.getCurrentUser();

                 /* This is the section where the images is converted, saved, and uploaded. I have not been able Locate the image from the ImageView, where the user uploads the picture to imageview from either their gallery and later on from facebook */ 
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                        /*to be retrieved from image view */);
                // Convert it to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] image = stream.toByteArray();

                // Create the ParseFile
                ParseFile file = new ParseFile("profilePicture.png", image);
                // Upload the image into Parse Cloud


                // Create a column named "Profile Picture" and set the string
                currentUser.put("ImageName", "Profile Picture");

                // Create a column named "ImageFile" and insert the image
                currentUser.put("ProfilePicture", file);

                // Create the class and the columns

                currentUser.put("name", name); 
                currentUser.put("age", age); 
                currentUser.put("headline", headline); 
                currentUser.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        setProgressBarIndeterminateVisibility(false);

                        if (e == null) {
                            // Success!
                            Intent intent = new Intent(ProfileCreation.this, MoodActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                        }
                        else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                            builder.setMessage(e.getMessage())
                                .setTitle(R.string.signup_error_title)
                                .setPositiveButton(android.R.string.ok, null);
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    }
                });
            }
        }
    });

If you could assist me in anyway, it would be truly helpful. If you require further clarification, let me know. Thanks in advance.

Upvotes: 0

Views: 950

Answers (2)

Mayank Jindal
Mayank Jindal

Reputation: 375

You can define a new class called util.java and in that class you would write a method

 public static byte[] getbytearray(Bitmap bm){
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
      byte[] byteArray = stream.toByteArray();
      return byteArray;
  }

Now you don't need to these basic lines again and again. And more important thing you should use asynctask to avoid out of memory error like -

public class storeimage extends AsyncTask<Bitmap,Void,Void>{

        @Override
        protected Void doInBackground(Bitmap... params) {
            byte[] byteArray = util.getbytearray(params[0]);
            ParseUser user = ParseUser.getCurrentUser();
            ParseFile parseFile = new ParseFile(user.getUsername()+"dp.png",byteArray);
            parseFile.saveInBackground();
            user.put("dp", parseFile);
            user.saveInBackground();
            Log.d("mayank","asynctask successful");

          //  Bitmap dpbitmap2 = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
            //ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
          //  dpbitmap.compress(Bitmap.CompressFormat.PNG, 0, stream2);
           // byte[] byteArray2 = stream2.toByteArray();
           // ParseFile parseFile2 = new ParseFile(user.getUsername()+"dp_circle.png",byteArray2);
           // ParseObject dp = new ParseObject("dp");
           // dp.put("pic",byteArray2);
           // dp.pinInBackground();
            return null;
        }
    }

after this
you should write

 new storeimage().execute(BitmapFactory.decodeFile(picturePath));

Upvotes: 0

tyczj
tyczj

Reputation: 73741

you forgot to save the file first

ParseFile file = new ParseFile("profilePicture.png", image);
file.saveinBackground()

Upvotes: 0

Related Questions