Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot cast from Byte to byte[] java

I am trying to convert byte array into image in android. I have a ArrayList<Byte> arrays; that contains all byte arrays that I want to convert. Now at

byt = (byte[]) arrays.get(0); 

its giving me Cannot cast from Byte to byte[] java exception.

        byte[] byt;
        byt = new byte[4096];
        byt = (byte[]) arrays.get(0) ;
        BufferedImage bImageFromConvert = null;
        InputStream in = new ByteArrayInputStream(byt);

        try {
            bImageFromConvert = ImageIO.read(in);
            SaveImages();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

How can I solve this?

like image 617
yakhtarali Avatar asked Jun 23 '26 05:06

yakhtarali


1 Answers

The ArrayList doesn't contain Byte[] or byte[] values, however the list itself can be converted to a byte array as it contains individual byte values

        byte[] bytes = new byte[array.size()];
        for (int i = 0; i < array.size(); i++) {
            bytes[i] = array.get(i);
        }
like image 123
user3217028 Avatar answered Jun 24 '26 19:06

user3217028