Search code examples
androidimageparse-platformandroid-drawable

How to upload an image in parse server using parse api in android


I want to upload an image in parse cloud server in android. But I am unable to do so.

I have tried the following code:

Drawable drawable = getResources().getDrawable(R.drawable.profilepic) ;
Bitmap bitmap = (Bitmap)(Bitmap)drawable()
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] data = stream.toByteArray();                

ParseFile imageFile = new ParseFile("image.png", data);
imageFile.saveInBackground();

Please let me know how can I do it.


Solution

  • Save ParseObject in the background

    // ParseObject
      ParseObject pObject = new ParseObject("ExampleObject");
      pObject.put("myNumber", number);
      pObject.put("myString", name);
      pObject.saveInBackground(); // asynchronous, no callback
    

    Save in the background with callback

    pObject.saveInBackground(new SaveCallback () {
       @Override
       public void done(ParseException ex) {
        if (ex == null) {
            isSaved = true;
        } else {
            // Failed
            isSaved = false;
        }
      }
    });
    

    Variations of the save...() method include the following:

        saveAllinBackground() saves a ParseObject with or without a callback.
        saveAll(List<ParseObject> objects) saves a list of ParseObjects.
        saveAllinBackground(List<ParseObject> objects) saves a list of ParseObjects in the 
        background.
        saveEventually() lets you save a data object to the server at some point in the future; use 
        this method if the Parse cloud is not currently accessible.
    

    Once a ParseObject has been successfully saved on the Cloud, it is assigned a unique Object-ID. This Object-ID is very important as it uniquely identifies that ParseObject instance. You would use the Object-ID, for example, to determine if the object was successfully saved on the cloud, for retrieving and refreshing a given Parse object instance, and for deleting a particular ParseObject.

    I hope you will solve your problem..