0

I am trying to upload pictures that the user has chosen onto an array in Parse.com. I was unable to do that. the problem is how to save and how do i get those photos back? this is my code for uploading the pictures (this is an example so i have used pics from drawable:

       Button btnadd = (Button) findViewById(R.id.btnadd);
    btnadd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Bitmap bitmap;
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.one);
            myArrayOfParseFiles.add(add(bitmap));
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.two);
            myArrayOfParseFiles.add(add(bitmap));
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.three);
            myArrayOfParseFiles.add(add(bitmap));

            ParseObject object = ParseObject.create("Pictures");
            object.add("pic", myArrayOfParseFiles);
            object.put("username", ParseUser.getCurrentUser().getUsername());
            object.saveInBackground();
        }
    });
    Button btnnext = (Button) findViewById(R.id.btnnext);
    btnnext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentObj = new Intent(Welcome.this, Show.class);
            startActivity(intentObj);
            finish();
        }
    });
}
    public ParseFile add(Bitmap pic){
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        pic.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] image = stream.toByteArray();
        ParseFile file = new ParseFile(image);
        file.saveInBackground();
    return file;

}

This is my code for getting the array and the picture. I'm getting null and I'm unable to get the picture. and also, how do i do that in a loop for the entire array i have on parse(in that specific raw) and put it in a bitmap list.

ParseQuery<ParseObject> query = ParseQuery.getQuery("Pictures");
            query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
            query.getFirstInBackground(new GetCallback<ParseObject>() {
                @Override
                public void done(ParseObject object, ParseException e) {
                    List<ParseFile> list = object.getList("pic");
                    try {
                        URL url = new URL(list.get(0).getUrl());
                        Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                        picone.setImageBitmap(bmp);
                    } catch (MalformedURLException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });

1 Answer 1

1

convert bitmap to string for upload:

    private String getStringFromBitmap(Bitmap bitmapPicture) {

        final int COMPRESSION_QUALITY = 100;
        String encodedImage;
        ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
        bitmapPicture.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY,
                byteArrayBitmapStream);
        byte[] b = byteArrayBitmapStream.toByteArray();
        encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
        return encodedImage;
    }

and convert your string to bitmap:

private Bitmap getBitmapFromString(String jsonString) {
    byte[] decodedString = Base64.decode(jsonString, Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    return decodedByte;
}

you can save your string in file and save it on server side or ... and then download string and convert it to bitmap again...

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

1 Comment

Thanks but the problem is I don't know how to download a file from an array in parse.

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.