Liraz Shaka Amir
Liraz Shaka Amir

Reputation: 649

Parse.com get picture from array

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();
                    }
                }
            });

Upvotes: 0

Views: 77

Answers (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...

Upvotes: 1

Related Questions