Search code examples
androidparse-platform

Parse.com & Android: How do I save Images to my parse database so that i can use them as a profile picture


I am new at android as well as parse.com. I need a way in which a user can click a button to take a photo or another button to choose a photo from the gallery. Then store the image in my parse.com database so that I can manipulate it with other events in the system. So far I am able to have the user update his status and save to my parse database but I do not know how to manipulate images and ImageViews. Here is my code so far:

public class UpdateActivity extends Activity {

    protected EditText mUpdateStatus;
    protected Button mUpdateStatusButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);

        //initialization of variables
        mUpdateStatus=(EditText)findViewById(R.id.updateStatusUpdate);
        mUpdateStatusButton=(Button)findViewById(R.id.updateButtonUpdate);

        //code update button click event
        mUpdateStatusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Get current user
                ParseUser currentUser=ParseUser.getCurrentUser();//Identifies current user
                String currentUserUsername=currentUser.getUsername();//stores username in variable

                //Create new variable to store strings
                String newStatus=mUpdateStatus.getText().toString();

                //Event for an empty status
                if (newStatus.isEmpty())
                        {AlertDialog.Builder builder=new AlertDialog.Builder(UpdateActivity.this);
                        builder.setMessage("STATUS SHOULD NOT BE EMPTY.");
                        builder.setTitle("OOPS!");
                        builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        AlertDialog dialog=builder.create();
                        dialog.show();}
                else{
                //Save the status in Parse.com
                ParseObject statusObject = new ParseObject("Status");//Create a new parse class
                statusObject.put("newStatus",newStatus);//Creates a new attribute and adds value from newStatus
                statusObject.put("User",currentUserUsername);//Stores username in new parse class

                //Save data and initiate callback method
                statusObject.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if(e==null)
                            {//Event for a Successful storage
                                Toast.makeText(UpdateActivity.this,getString(R.string.succssfulUpdate),Toast.LENGTH_LONG).show();

                                //Take user back to profile
                                Intent main = new Intent(UpdateActivity.this, ProfileActivity.class);
                                UpdateActivity.this.startActivity(main);

                            }
                        else
                            {//Event for an Unsuccessful storage
                                AlertDialog.Builder builder=new AlertDialog.Builder(UpdateActivity.this);
                                builder.setMessage(e.getMessage());
                                builder.setTitle("SORRY!");
                                builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                                AlertDialog dialog=builder.create();
                                dialog.show();
                            }
                    }
                });}

            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_update, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch(id) {
            case R.id.logoutUpdateMenu:
            {//logout the user
                ParseUser.logOut();
                //Take user back to login
                Intent intent = new Intent(UpdateActivity.this, LoginActivity.class);
                UpdateActivity.this.startActivity(intent);
                UpdateActivity.this.finish();
                Toast.makeText(getApplicationContext(), getString(R.string.logout_text), Toast.LENGTH_LONG).show();
                break;}
            }

        return super.onOptionsItemSelected(item);
    }
}

Solution

  • I have no experience with parse.com but If you are able to put images(bit map) into ParseObject, you just need to call take photo or pick photo action using intent and startActivityForResult. Example:

    public void onClickTakePhoto(View view) {
        dispatchTakePictureIntent();
    }
    public void onClickPickPhoto(View view) {
        Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, REQUEST_SELECT_IMAGE);
    }
    //Code from Android documentation
    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if ((requestCode == REQUEST_IMAGE_CAPTURE || requestCode == REQUEST_SELECT_IMAGE) && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            ParseObject statusObject = new ParseObject("Status");
            //I think parse has similar support If not this
            statusObject.put("profile_photo",imageBitmap);
            statusObject.saveInBackground( new Callback(){...});
    
        }
    }