2

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?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
yakhtarali
  • 432
  • 13
  • 30

10 Answers10

1

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

Try using ByteArrayOutputStream instead of an arraylist, its better. Then you use ByteArrayOutputStream.toByteArray().

Junior
  • 17
  • 1
  • 6
  • The user is getting an arraylist as input if I understand correctly. This fix is liekly untenable in practice. – nanofarad Jan 14 '14 at 12:27
0

ArrayList<Byte> arrays; does not contain a list of `Byte[]'. It is a single list of boxed Byte objects.

You will need to do a conversion by hand:

 byte[] byteArray = new byte[arrays.size()];

 for (int i=0;i<byteArray.length;i++) {
    byteArray[i] = arrays.get(i);
 }

The loop will unbox the Byte to a byte for you as it is placed into the array.

RonK
  • 9,472
  • 8
  • 51
  • 87
Tim B
  • 40,716
  • 16
  • 83
  • 128
  • array.length in the for-loop declaration doesn't compile, it needs to be arrays.length() or byteArray.length. – arne.jans Jan 14 '14 at 12:40
  • The OP also indicated that he has an ArrayList of arrays, so your solution should reflect that, I think. – arne.jans Jan 14 '14 at 12:42
  • The OP also indicated that it is a ArrayList – Daniel Stucki Jan 14 '14 at 12:45
  • @arne.jans Thanks, I renamed the byteArray but forgot to rename it in the loop. The OP may have said in his post ArrayList of arrays but that's not what the type he said in his code is so I think it's more likely he's misunderstood that than the other way around. – Tim B Jan 14 '14 at 13:15
0

You seem to be misusing the ImageIO API (correct me if I misunderstood):

You can use ImageIO.read with 3 different inputs:

  • URL
  • File
  • InputStream

If you have a file to load - just pass it like so:

bImageFromConvert = ImageIO.read(new File("someFile.jpg"));

Or like this if it is within your application:

bImageFromConvert =
   ImageIO.read(this.getClass().getClassloader().getResourceAsStream("com/myapp/images/someFile.jpg"));

If you are getting the data as bytes from an outside source - try using a byte[] instead of List<Byte> which I assume you are using as it is more fitting to the problem you are trying to solve.

Also, note that you allocated only 4K for the image - and this might not be enough for loading your image - which (should) cause ImageIO.read to fail.

I'm not adding anything about using a URL - but I assume it is obvious.

If this does not answer your question please add more relevant inputs.

RonK
  • 9,472
  • 8
  • 51
  • 87
  • i want to transfer images from my wcf ksoap2 service to android. so i have converted images to byte array and passed to android. here ArrayList contains all that byte arrays. here i want to convert byte array to image – yakhtarali Jan 14 '14 at 13:09
  • So you read a byte[] from some service and then you convert it into an ArrayList and then you want to convert it back to be byte[]? – RonK Jan 14 '14 at 13:12
  • If I understand correctly - you get the image from the SOAP API as a String, probably Base64 encoded - am I right? If so, you can convert the Base64 Encoded string into a byte[] and just go with that – RonK Jan 14 '14 at 13:15
  • i filled ArrayList from my service, it contains converted images that are in byte array form. now i want to convert them back to images – yakhtarali Jan 14 '14 at 13:17
  • If you have no alternative other than getting an `ArrayList` follow TimB's answer to convert it into a `byte[]` - http://stackoverflow.com/a/21113724/357360 – RonK Jan 14 '14 at 13:25
  • will you please help regarding passing images from wcf ksoap2 to android. i have to transfer 10-15 images at a time. now i have a new problem, my service is responding correctly but my ArrayList is not filling. will you please help? – yakhtarali Jan 14 '14 at 13:45
  • Please post a new question as this is off-topic – RonK Jan 14 '14 at 13:47
  • please check this http://stackoverflow.com/questions/21115575/trouble-in-receiveing-byte-from-wcf-ksoap2-to-android – yakhtarali Jan 14 '14 at 14:10
0

As I understand you are trying to get an image from soap service to your android device.

I suggest to use the following method,

Read the SOAP object property as a String.

String mPhoto;

 if (soapObject.hasProperty(PHOTO)) {
        Object obj = soapObject.getProperty(PHOTO);
        if (obj != null && obj.getClass().equals(SoapPrimitive.class)) {
            SoapPrimitive j = (SoapPrimitive) obj;
            mPhoto = j.toString();
        } else if (obj != null && obj instanceof String) {
            mPhoto = obj.toString();
        } else {
            mPhoto = null;
        }
    }

Then convert that String to byte[]

public byte[] getPhoto() {
    if (mPhoto != null) {
           // org.kobjects.base64.Base64;
        return Base64.decode(mPhoto);
    } else {
        return null;
    }
}

Decoding is important to reconstruct the same byte[] sent from the service.

naveejr
  • 735
  • 1
  • 15
  • 31
0

If you couldn't use another solution with Byte[] instead. Then you could use toPrimitive method from ArrayUtils class from Apache Lang library. Or use this bellow code:

public static byte[] toPrimitive(final Byte[] array) {
        if (array == null) {
            return null;
        } else if (array.length == 0) {
            return EMPTY_BYTE_ARRAY;
        }
        final byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].byteValue();
        }
        return result;
    }
SangDang
  • 11
  • 3
0

You have to declare your ArrayList<Byte> as ArrayList<byte[]> arrays;.

Your current declaration, as it is, does not store a list of byte arrays, but a list of Byte objects, not arrays. Even if your arrays were declared as ArrayList<Byte[]> arrays;, you cannot cast a Byte[] array to a byte[] array. Java does not support directly casting primitive data types and their wrapper types, nor their array types.

Primitives and their wrapper types are boxed or un-boxed. The compiler silently replaces declarations such as Integer myInt = 0; with Integer myInt = Integer.valueOf(0);, and int myInt = myWrappedInt; with int myInt = myWrappedInt.intValue(); because of the auto-(un)boxing feature introduced in Java 5.

AMDG
  • 1,118
  • 1
  • 13
  • 29
0

You can use the ArrayList.toArray(T[]) method, like so:

Byte[] byteArray = new Byte[arrays.size()];
ArrayList.toArray(byteArray);

This method uses System.arrayCopy, a native method. It is a lot faster than iterating through your whole ArrayList.

Genos
  • 413
  • 4
  • 12
-1

The error occurs on line 3

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

arrays.get(0) returns a Byte which you then try to cast to a byte[]

Daniel Stucki
  • 203
  • 1
  • 7
-1

If you tried to put byte-arrays into the ArrayList arrays, then you should correct its declaration to:

ArrayList<Byte[]> arrays = new ArrayList<Byte[]>();

and fill it with your data.

arne.jans
  • 3,798
  • 2
  • 22
  • 27
  • The first example isn't even correct Java, and what you are trying to do isn't possible with primitive arrays. – Keppil Jan 14 '14 at 12:31