lawonga
lawonga

Reputation: 842

Saving image/files into Parse Cloud using Parse Code

I have this code (Java/Android) that takes the bitmap, converts it to byte array, and then puts it into a Map known as 'picture' and is finally sent up via calling the "createcard" function:

    public static void createCard(final String nametext, final String initialbalancetext, String databaseclass, String cardnotes, String cardtype, Bitmap picture) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    picture.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    Map<String, Object> map = new HashMap<>();
    map.put("cardname", nametext);
    map.put("balance", Double.valueOf(initialbalancetext));
    map.put("database", databaseclass);
    map.put("cardnotes", cardnotes);
    map.put("picture", byteArray);
    // Check if network is connected before creating the card
    if (CardView.isNetworkConnected) {
        ParseCloud.callFunctionInBackground("createcard", map, new FunctionCallback<String>() {
            @Override
            public void done(String s, ParseException e) {
                // COMPLETE
                CardListCreator.clearadapter();
                CardListAdapter.queryList();
            }
        });
    }

This is my "createcard" function (Javascript/Parse Cloudcode). It supposedly takes the 'picture' key and grabs the byte array, and attempts to save it:

Parse.Cloud.define("createcard", function(request, response){
var cardname = request.params.cardname;
var balance = request.params.balance;
var database = request.params.database;
var cardnotes = request.params.cardnotes;
var picture = request.params.picture;

var picturename = "photo.jpg";
var parseFile = new Parse.File(picturename, picture);
parseFile.save().then(function(parseFile){
    var currentuser = Parse.User.current();
    var CurrentDatabase = Parse.Object.extend(database);

    var newObject = new CurrentDatabase;
    newObject.set("cardname", cardname);
    newObject.set("balance", balance);
    newObject.set("user", currentuser);
    newObject.set("cardnotes", cardnotes);
    newObject.set("cardpicture", parseFile);
    newObject.save(null, {
        success: function(newObject){
            console.log(newObject);
            response.success(newObject + " successfully created");
        }, error: function(newObject, error){
            console.log(error);
            response.error(error+" error");
        }
    })
});
});

Now my problem is that the function is simply not working. I don't know why as my console.log isn't actually logging anything. Does anyone have any ideas?

Upvotes: 1

Views: 165

Answers (2)

lawonga
lawonga

Reputation: 842

For all in the future: You want to put the image into the byte[] and put it into the map. You don't need to encode it to a string, unless you want to decode it in the cloudcode. On the cloudcode, request the param of the byte[] that you uploaded, and then put it into the ParseFile and then finally call save on it.

Upvotes: 0

Abhinav singh
Abhinav singh

Reputation: 1466

I hope this code is given your question.

   private void Update(){

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Table Name");
    query.getInBackground(parsekey, new GetCallback<ParseObject>() {
        public void done(ParseObject gameScore, ParseException e) {


            if (e == null) {

                if(camera!=null) {
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    camera.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    byte imageInByte[] = stream.toByteArray();
                    encodedImage = Base64.encodeToString(imageInByte, Base64.DEFAULT);
                    image = new ParseFile("image.txt", imageInByte);                  

                }else{
                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.user_icon);
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    byte imageInByte[] = stream.toByteArray();
                    encodedImage = Base64.encodeToString(imageInByte, Base64.DEFAULT);
                    image = new ParseFile("image.txt", imageInByte);
                }




              findViewById(R.id.editName)).getText().toString(),Profile.this);
            gameScore.put("name", ((EditText) findViewById(R.id.editName)).getText().toString().trim());
            gameScore.put("gender",gender_val);
            gameScore.put("country",country_val);
            gameScore.put("dateofbirth",((TextView) findViewById(R.id.editdate)).getText().toString().trim());
            gameScore.put("profession",profession);
            gameScore.put("image",image);

            gameScore.saveInBackground();


                Toast.makeText(Profile.this,"SuccessFull Updated",Toast.LENGTH_LONG).show();

            }else{
                Log.e("erroreeeee", DataBase.getUserObjectId(Profile.this)+"  "+e.getMessage());
            }

        }

    });
}

Upvotes: 3

Related Questions