3
votes

I have a byte[] which is returned by fetching data from some table. I want to convert this byte array into png image. Note: I do not have byte array from an image as in similar examples elsewhere.

 byte[] bytes = modelDashboardService.exportMyQueuePNG("pp83900","1");

InputStream in = new ByteArrayInputStream(bytes);

BufferedImage bImageFromConvert = ImageIO.read(in);//ImageIO.read(url);//

if(bImageFromConvert != null){
  ImageIO.write(bImageFromConvert, "png", new File(
  "D:/test1.png"));

}

My byte[] is not null but my BufferedImage is null .

please help.

Byte[] is : [37, 80, 68, 70, 45, 49, 46, 55, 10, 37, -30, -29, -49, -45, 10, 51, 32, 48, 32, 111, 98, 106, 10, 60, 60, 47, 76, 101, 110, 103, 116, 104, 32, 52, 57, 50, 47, 70, 105, 108, 116, 101, 114, 47, 70, 108, 97, 116, 101, 68, 101, 99, 111, 100, 101, 62, 62, 115, 116, 114, 101, 97, 109, 10, 120, -100, -115, -107, 65, 111, -29, 32, 16, -123, -17, -2, 21, 115, -20, 30, 66, -127, 1, -116, -113, -39, -124, -83, -68, 90, -59, -87, 67, -38, -12, -76, 90, -75, 77, -91, 85, -93, -86, -67, -12, -17, -17, 24, -127, 55, -119, 113, -99, 67, 34, -20, -9, -15, 24, 63, 6, -101, -125, -128, -103, 0, 14, -70, -46, -12, -1, 120, 40, -34, -117, -17, -66, -96, 107, -91, -63, 63, -47, 96, -90, 120, 55, 112, -66, -72, 37, -115, 51, 11, -35, 79, -64, -57, 75, 97, 21, -109, -96, -71, 6, -93, -104, -48, 64, -32, -57, 115, -79, 63, 101, -124, -78, 12, 117, -96, 80, 50, 110, 71, 40, -53, -103, -62, 64, 9, 73, 19, -14, 20, 114, -53, 74, -100, 90, 17, 75, -55, 108, 53, 69, 41, 44, 25, 87, 83, -108, -26, -126, -119, 73, 47, 109, 52, -61, 73, 47, 35, 43, -90, 39, -95, 10, -103, -55, 81, 93, -8, 18, 126, -46, -122, -36, -48, 12, 13, -97, -7, -12, 55, 81, 28, -119, -67, -105, -13, 121, 39, 121, 36, -24, 94, -50, 39, -100, -28, -111, 104, -109, 60, -110, 105, 47, -25, -61, 76, 114, 62, -59, 94, -51, -57, -73, -95, -8, -88, -85, -69, 62, -25, -108, -77, 53, 93, 114, 40, -64, 31, -118, -21, 31, -126, 34, 0, -65, 47, -82, -36, -50, 45, -74, -66, -2, -26, -1, -98, -111, 84, 45, -111, 87, -51, 10, -4, -61, -38, 117, 58, -99, -122, 35, 63, -95, 121, 8, 123, -32, 88, 47, 51, -80, -107, 33, -6, 1, -68, 104, 86, -66, 109, 126, -63, 124, -31, -21, -69, -38, 63, 12, -89, -94, -32, 97, 91, 6, 83, 55, 126, -34, -6, -29, -70, 19, 25, 43, 95, -50, 125, -90, 108, 44, 85, -40, -58, -127, -35, 114, -21, 78, -52, 34, -9, -91, -103, -62, 42, 108, -6, -40, 99, 29, 27, 38, 54, 26, -34, -76, -51, 118, -3, -69, -55, 17, -68, 12, -60, -3, -54, -75, -61, 37, 53, -57, -48, 72, -105, 44, -103, -40, -72, -92, -37, -47, 78, 55, 57, 79, 83, -122, -18, -69, -56, 51, -78, -47, -77, 117, 119, -75, -69, 119, 57, 34, 62, 70, 102, 61, -125, -126, -27, 58, 39, -77, 92, 68, -113, 67, -53, -8, 85, 58, 28, -128, -117, 12, 35, 27, 29, -87, -119, -4, 118, 19, 45, -33, 51, 47, 28, -28, -35, 113, 58, 20, 88, -123, -63, -21, -1, -9, 65, 21, -107, 82, -97, 41, -15, 6, 41, -74, 87, 110, 79, -21, 53, -52, 32, 72, 115, 86, -18, -6, -49, -53, 51, -55, 111, -5, 84, 15, -92, 25, -99, -91, 84, -12, -91, -126, -21, -35, 94, -64, -14, 13, 78, 12, -93, 76, 110, -40, -35, -96, 47, -41, 44, 13, -56, -26, 31, 113, 64, 107, -53, 10, 101, 110, 100, 115, 116, 114, 101, 97, 109, 10, 101, 110, 100, 111, 98, 106, 10, 53, 32, 48, 32, 111, 98, 106, 10, 60, 60, 47, 80, 97, 114, 101, 110, 116, 32, 52, 32, 48, 32, 82, 47, 67, 111, 110, 116, 101, 110, 116, 115, 32, 51, 32, 48, 32, 82, 47, 84, 121, 112, 101, 47, 80, 97, 103, 101, 47, 82, 101, 115, 111, 117, 114, 99, 101, 115, 60, 60, 47, 88, 79, 98, 106, 101, 99, 116, 60, 60, 47, 88, 102, 49, 32, 49, 32, 48, 32, 82, 62, 62, 47, 70, 111, 110, 116, 60, 60, 47, 70, 49, 32, 50, 32, 48, 32, 82, 62, 62, 62, 62, 47, 77, 101, 100, 105, 97, 66, 111, 120, 91, 48, 32, 48, 32, 53, 57, 53, 32, 56, 52, 50, 93, 47, 82, 111, 116, 97, 116, 101, 32, 57, 48, 62, 62, 10, 101, 110, 100, 111, 98, 106, 10, 50, 32, 48, 32, 111, 98, 106, 10, 60, 60, 47, 66, 97, 115, 101, 70, 111, 110, 116, 47, 72, 101, 108, 118, 101, 116, 105, 99, 97, 47, 84, 121, 112, 101, 47, 70, 111, 110, 116, 47, 69, 110, 99, 111, 100, 105, 110, 103, 47, 87, 105, 110, 65, 110, 115, 105, 69, 110, 99, 111, 100, 105, 110, 103, 47, 83, 117, 98, 116, 121, 112, 101, 47, 84, 121, 112, 101, 49, 62, 62, 10, 101, 110, 100, 111, 98, 106, 10, 49, 32, 48, 32, 111, 98, 106, 10, 60, 60, 47, 84, 121, 112, 101, 47, 88, 79, 98, 106, 101, 99, 116, 47, 82, 101, 115, 111, 117, 114, 99, 101, 115, 60, 60, 47, 70, 111, 110, 116, 60, 60, 47, 70, 49, 32, 50, 32, 48, 32, 82, 62, 62, 62, 62, 47, 83, 117, 98, 116, 121, 112, 101, 47, 70, 111, 114, 109, 47, 66, 66, 111, 120, 91, 48, 32, 48, 32, 51, 48, 32, 49, 54, 93, 47, 77, 97, 116, 114, 105, 120, 32, 91, 49, 32, 48, 32, 48, 32, 49, 32, 48, 32, 48, 93, 47, 76, 101, 110, 103, 116, 104, 32, 52, 52, 47, 70, 111, 114, 109, 84, 121, 112, 101, 32, 49, 47, 70, 105, 108, 116, 101, 114, 47, 70, 108, 97, 116, 101, 68, 101, 99, 111, 100, 101, 62, 62, 115, 116, 114, 101, 97, 109, 10, 120, -100, 43, -28, 114, 10, -31, 50, 84, 48, 0, 66, 67, 5, 35, 32, 12, -55, -27, -46, 119, 51, 84, 48, 4, -78, -46, -72, 52, 12, 53, 67, -78, -72, 92, 67, -72, 2, -71, 0, -88, 92, 8, 34, 10, 101, 110, 100, 115, 116, 114, 101, 97, 109, 10, 101, 110, 100, 111, 98, 106, 10, 52, 32, 48, 32, 111, 98, 106, 10, 60, 60, 47, 84, 121, 112, 101, 47, 80, 97, 103, 101, 115, 47, 67, 111, 117, 110, 116, 32, 49, 47, 75, 105, 100, 115, 91, 53, 32, 48, 32, 82, 93, 62, 62, 10, 101, 110, 100, 111, 98, 106, 10, 54, 32, 48, 32, 111, 98, 106, 10, 60, 60, 47, 84, 121, 112, 101, 47, 67, 97, 116, 97, 108, 111, 103, 47, 80, 97, 103, 101, 115, 32, 52, 32, 48, 32, 82, 62, 62, 10, 101, 110, 100, 111, 98, 106, 10, 55, 32, 48, 32, 111, 98, 106, 10, 60, 60, 47, 80, 114, 111, 100, 117, 99, 101, 114, 40, 105, 84, 101, 120, 116, -82, 32, 53, 46, 53, 46, 53, 32, -87, 50, 48, 48, 48, 45, 50, 48, 49, 52, 32, 105, 84, 101, 120, 116, 32, 71, 114, 111, 117, 112, 32, 78, 86, 32, 92, 40, 65, 71, 80, 76, 45, 118, 101, 114, 115, 105, 111, 110, 92, 41, 41, 47, 77, 111, 100, 68, 97, 116, 101, 40, 68, 58, 50, 48, 49, 54, 48, 51, 50, 51, 49, 54, 51, 54, 53, 55, 43, 48, 53, 39, 51, 48, 39, 41, 47, 67, 114, 101, 97, 116, 105, 111, 110, 68, 97, 116, 101, 40, 68, 58, 50, 48, 49, 54, 48, 51, 50, 51, 49, 54, 51, 54, 53, 55, 43, 48, 53, 39, 51, 48, 39, 41, 62, 62, 10, 101, 110, 100, 111, 98, 106, 10, 120, 114, 101, 102, 10, 48, 32, 56, 10, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 32, 54, 53, 53, 51, 53, 32, 102, 32, 10, 48, 48, 48, 48, 48, 48, 48, 56, 48, 54, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 48, 48, 55, 49, 56, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 48, 48, 48, 49, 53, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 48, 49, 48, 50, 50, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 48, 48, 53, 55, 52, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 48, 49, 48, 55, 51, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 48, 49, 49, 49, 56, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 116, 114, 97, 105, 108, 101, 114, 10, 60, 60, 47, 82, 111, 111, 116, 32, 54, 32, 48, 32, 82, 47, 73, 68, 32, 91, 60, 98, 101, 55, 48, 97, 57, 52, 52, 57, 97, 102, 57, 50, 56, 55, 97, 54, 48, 100, 99, 53, 98, 57, 97, 48, 57, 48, 101, 49, 50, 49, 54, 62, 60, 98, 101, 55, 48, 97, 57, 52, 52, 57, 97, 102, 57, 50, 56, 55, 97, 54, 48, 100, 99, 53, 98, 57, 97, 48, 57, 48, 101, 49, 50, 49, 54, 62, 93, 47, 73, 110, 102, 111, 32, 55, 32, 48, 32, 82, 47, 83, 105, 122, 101, 32, 56, 62, 62, 10, 37, 105, 84, 101, 120, 116, 45, 53, 46, 53, 46, 53, 10, 115, 116, 97, 114, 116, 120, 114, 101, 102, 10, 49, 50, 55, 53, 10, 37, 37, 69, 79, 70, 10]

3
what type of data you are having in bytes?afzalex
What do you mean by "I do not have a byte array as in similar examples"? What is different?lisa p.
@lisa p as the byte array fetched from another imageuser2959556

3 Answers

1
votes

Try out below code:

 private void saveImage(String path, final byte[] bytes) throws IOException {
        final File file = new File(path);
        final FileOutputStream fileOut = new FileOutputStream(file );
        fileOut.write(bytes);
        fileOut.flush();
        fileOut.close();
    }
1
votes

According to the documentation of ImageIO.read(InputStream input):

Returns a BufferedImage as the result of decoding a supplied InputStream with an ImageReader chosen automatically from among those currently registered. The InputStream is wrapped in an ImageInputStream. If no registered ImageReader claims to be able to read the resulting stream, null is returned.

My best guess is that the byte array you're retrieving from the table is not from a (valid) image.

1
votes

use it:

File file = new File(getFilesDir()+"/file.png");
FileOutputStream fos = new FileOutputStream(file);

//write your byteArray here
fos.write(byteArray);
fos.flush();
fos.close();

see this link