0

I have a JSONArray that contains image files in Base64 format.

I would like to decode the images in Byte list.

My json array format:

jsonarray:[{"manchine_image":{"image_file":"VW5pdHlGUw...}

My existing code:


 List<String> image = new ArrayList<String>();   
                    for(int i = 0; i < jsonarray.length();i++) {

                          JSONObject jsonobject = jsonarray.getJSONObject(i);
                          image.add(new String(jsonobject.toString().getBytes("image_file")));
}

I tried this but i have this error:

java.io.UnsupportedEncodingException: image_file

Thanks anyway.

2 Answers 2

1

This is how you can decode a Base64 String to byte array :

import java.util.Base64;

class Scratch {
    public static void main(String[] args) {
        String imageFile = "VW5pdHlGUw...";
        byte[] decode = Base64.getDecoder().decode(imageFile);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I found the answer. Thank you for your help.

image.add(new String(jsonobject.toString().getBytes("image_file"))); --> image.add(jsonobject.getJSONObject("machine_image").getString("image_file")); //correct

Comments

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.