-2

The C# server returns an array of bytes

E.g.:

       byte[] bytearray = new byte[]{255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,72,0,72,0,0,255,225,0,88,69,120,105,102,0,0,77,77,0,42,0,0,0,8,0,2,1,18,0,3,0,0,0,1,0,1,0,0,135,105,0,4,0,0,0,1,0,0,0,38,0,0,0,0,0,3,160,1,0,3,0,0,0,1,0,1,0,0,160,2,0,4,0,0,0,1,0,0,0,41,160,3,0,4,0,0,0,1,0,0,0,24,0,0,0,0,255,237,0,56,80,104,111,116,111,115,104,111,112,32,51,46,48,0,56,66,73,77,4,4,0,0,0,0,0,0,56,66,73,77,4,37,0,0,0,0,0,16,212,29,140,217,143,0,178,4,233,128,9,152,236,248,66,126,255,192,0,17,8,0,24,0,41,3,1,17,0,2,17,1,3,17,1,255,196,0,31,0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,255,196,0,181,16,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,125,1,2,3,0,4,17,5,18,33,49,65,6,19,81,97,7,34,113,20,50,129,145,161,8,35,66,177,193,21,82,209,240,36,51,98,114,130,9,10,22,23,24,25,26,37,38,39,40,41,42,52,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,131,132,133,134,135,136,137,138,146,147,148,149,150,151,152,153,154,162,163,164,165,166,167,168,169,170,178,179,180,181,182,183,184,185,186,194,195,196,197,198,199,200,201,202,210,211,212,213,214,215,216,217,218,225,226,227,228,229,230,231,232,233,234,241,242,243,244,245,246,247,248,249,250,255,196,0,31,1,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,255,196,0,181,17,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,119,0,1,2,3,17,4,5,33,49,6,18,65,81,7,97,113,19,34,50,129,8,20,66,145,161,177,193,9,35,51,82,240,21,98,114,209,10,22,36,52,225,37,241,23,24,25,26,38,39,40,41,42,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,130,131,132,133,134,135,136,137,138,146,147,148,149,150,151,152,153,154,162,163,164,165,166,167,168,169,170,178,179,180,181,182,183,184,185,186,194,195,196,197,198,199,200,201,202,210,211,212,213,214,215,216,217,218,226,227,228,229,230,231,232,233,234,242,243,244,245,246,247,248,249,250,255,219,0,67,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,255,219,0,67,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,255,221,0,4,0,6,255,218,0,12,3,1,0,2,17,3,17,0,63,0,252,179,241,79,237,247,127,226,253,38,219,75,248,127,119,163,104,126,13,212,111,191,178,45,180,200,239,109,180,123,205,98,194,237,44,109,239,53,9,111,88,36,227,78,129,225,187,79,38,237,158,91,184,23,54,195,236,247,81,6,252,238,120,140,119,53,76,36,41,207,13,47,101,57,213,173,120,175,103,40,210,171,39,70,173,74,147,132,37,42,171,145,185,164,148,37,122,114,110,222,255,0,223,97,178,60,44,101,75,25,90,172,113,115,83,167,245,122,42,50,147,146,115,132,57,169,82,140,101,43,69,171,70,43,87,204,154,186,75,151,251,22,255,0,130,38,124,40,210,252,15,251,30,124,33,191,184,211,34,147,89,241,79,133,111,188,105,37,221,205,148,176,106,18,233,26,246,185,170,106,190,25,180,116,120,227,146,59,75,109,18,234,210,104,32,120,210,60,79,36,219,100,121,89,219,210,194,212,196,206,42,181,109,43,213,140,61,188,147,82,188,148,35,27,41,110,210,81,178,105,181,179,86,189,229,203,139,140,35,57,83,135,50,165,78,164,213,56,203,71,27,205,202,73,195,145,56,187,191,133,164,215,117,177,242,23,252,20,183,246,171,248,107,240,243,198,222,41,191,248,85,240,179,194,208,124,91,127,28,233,186,23,197,13,118,102,155,195,22,222,43,251,15,131,226,151,74,213,245,217,244,205,50,238,93,119,88,211,180,214,208,244,155,61,70,242,47,182,13,44,67,97,37,244,182,118,86,80,69,225,113,102,58,142,2,134,19,19,86,157,106,181,170,87,116,33,236,125,159,52,163,200,231,43,243,73,69,168,56,189,126,39,204,246,209,195,222,225,220,21,92,92,170,65,85,140,41,170,74,163,114,92,214,113,154,133,162,147,178,186,147,186,77,246,122,171,199,249,207,248,221,251,98,252,67,248,121,125,225,255,0,26,248,171,225,199,195,143,30,162,157,102,194,214,203,197,87,154,150,170,182,186,190,173,115,164,235,81,107,146,220,67,97,163,201,38,161,18,248,117,44,210,105,210,86,91,52,138,221,60,152,32,72,171,229,56,111,19,131,204,113,152,234,184,106,85,233,99,42,198,21,43,226,235,206,53,43,212,163,74,82,133,58,84,218,118,133,26,126,218,110,20,96,185,19,156,154,140,92,167,41,122,152,206,29,195,209,88,106,184,154,235,23,10,14,175,179,167,82,148,37,24,215,175,42,85,39,136,114,228,187,170,222,30,49,246,147,139,169,201,24,195,153,66,48,71,180,120,83,246,242,248,223,227,191,7,248,127,196,111,31,195,31,14,219,248,146,25,46,96,179,176,240,214,189,120,208,92,125,162,229,46,162,123,137,252,79,111,19,191,155,4,242,9,146,0,179,150,44,118,59,109,172,49,185,228,178,252,118,39,7,71,10,228,240,181,108,229,42,209,132,101,206,149,71,37,78,52,158,173,212,92,254,242,106,82,230,110,91,30,205,12,170,157,106,84,234,58,178,181,72,222,203,91,91,69,239,107,162,75,173,175,165,175,202,142,235,254,26,147,227,31,253,12,126,12,255,0,194,86,239,255,0,154,26,211,253,104,197,255,0,208,52,63,240,58,132,255,0,98,225,255,0,231,236,255,0,240,58,103,255,208,252,18,253,190,190,39,126,203,22,191,180,79,195,215,253,158,110,60,19,99,240,246,123,77,5,126,34,105,126,1,177,130,215,65,182,134,219,93,179,142,225,98,190,142,48,5,229,214,132,247,145,93,182,155,50,98,56,45,165,114,46,11,181,124,124,242,90,217,131,134,34,180,39,39,24,168,168,85,169,83,146,92,151,229,106,138,106,13,187,253,181,125,244,122,163,245,12,6,117,71,46,163,82,132,101,5,46,102,233,214,167,74,156,234,46,104,218,222,213,251,235,150,73,62,88,201,37,229,117,19,251,250,253,155,191,105,127,135,209,233,126,29,139,195,154,157,172,26,68,126,29,209,34,209,237,44,124,179,13,190,137,46,153,111,253,139,29,186,194,74,173,171,105,194,210,59,80,3,34,194,209,230,78,77,93,36,160,227,30,137,37,203,217,43,165,162,237,174,150,81,86,182,141,62,95,18,188,39,46,105,59,201,202,82,147,155,221,182,239,43,190,183,222,238,90,189,116,220,252,110,253,166,60,15,240,103,226,215,252,20,130,47,135,94,63,241,182,163,105,225,127,143,159,15,47,254,50,248,62,202,239,87,183,208,116,207,248,72,116,6,240,255,0,130,252,73,225,171,29,77,25,110,174,245,23,139,73,77,70,29,36,157,150,182,26,162,77,6,25,228,219,227,103,249,28,243,26,241,173,136,138,169,151,225,227,31,99,78,157,94,90,144,197,73,218,172,228,185,85,211,140,85,166,219,229,111,151,147,83,220,203,51,74,120,12,181,83,195,57,195,50,117,156,106,185,83,83,165,60,47,189,42,114,140,155,74,50,140,159,43,138,90,242,222,77,237,45,95,219,19,254,8,239,240,11,226,7,192,207,28,219,248,6,235,226,5,143,196,203,111,15,106,190,33,248,123,0,241,29,156,246,23,254,43,211,52,91,193,164,105,207,109,171,207,107,101,53,182,165,35,13,50,65,45,204,113,91,173,225,186,243,23,203,87,110,60,179,35,203,242,186,142,190,14,149,79,106,233,201,74,53,42,74,106,81,231,85,121,28,181,105,202,112,140,121,175,123,45,149,153,219,131,206,234,226,113,152,92,62,103,82,17,192,85,175,78,150,38,189,56,114,212,161,78,173,233,203,17,30,72,201,205,225,212,253,178,166,213,167,200,225,173,236,127,25,30,34,212,255,0,107,47,130,62,15,176,210,117,123,157,3,79,131,195,254,53,241,119,195,229,240,244,83,104,26,191,137,180,189,91,195,26,165,229,150,186,247,150,246,115,220,50,105,15,170,38,163,103,103,170,172,146,91,220,155,121,124,153,196,77,6,255,0,102,174,79,193,121,134,105,137,246,148,241,43,20,232,81,196,86,168,231,90,158,29,182,148,84,97,47,134,85,33,200,156,160,151,103,105,59,242,196,234,241,110,3,9,74,105,208,157,23,93,211,162,146,161,82,179,167,56,42,244,235,78,9,62,90,117,105,213,139,142,173,198,238,19,228,107,148,183,255,0,11,187,227,255,0,253,59,127,224,61,167,255,0,28,173,255,0,176,120,91,254,159,127,224,117,63,249,89,203,253,161,196,255,0,243,230,135,254,11,137,255,209,254,87,227,255,0,130,78,255,0,193,70,138,220,72,255,0,240,79,223,219,145,64,220,99,135,254,25,83,227,202,134,220,79,202,168,191,15,247,133,10,64,0,179,22,192,39,111,69,243,227,139,197,183,78,63,87,146,90,115,73,210,149,214,155,234,146,230,111,93,109,215,85,107,159,101,245,124,185,66,114,120,202,23,73,242,65,87,166,226,223,110,94,117,37,20,150,157,118,118,87,63,170,95,216,175,225,103,237,135,224,159,135,63,178,214,157,226,207,217,55,246,185,208,60,87,115,240,190,219,194,62,35,183,213,127,103,15,141,182,75,161,220,252,50,185,58,93,156,158,42,188,184,240,82,90,248,125,245,93,44,64,52,148,215,38,179,125,102,41,36,123,4,158,56,164,116,242,241,24,122,234,173,105,66,141,89,46,119,47,118,13,223,218,94,94,237,163,239,90,252,175,149,233,125,121,108,220,186,105,98,112,146,163,24,203,19,135,141,163,167,53,122,122,37,21,23,118,228,157,214,141,94,73,233,162,149,143,12,255,0,130,205,126,199,31,183,23,196,75,223,130,122,159,194,47,217,87,246,142,241,165,239,135,60,77,241,45,46,47,252,17,240,11,226,215,139,47,52,125,47,90,176,240,77,254,143,43,63,135,124,45,125,113,101,25,146,194,230,219,124,143,18,73,123,13,202,39,204,142,169,117,232,251,84,165,83,15,139,159,42,140,99,78,148,28,121,185,148,92,185,220,162,214,150,209,219,77,85,213,212,141,240,88,202,84,185,163,71,27,151,211,82,75,154,117,234,197,242,186,110,92,188,138,51,73,182,155,90,233,102,159,42,78,241,252,175,210,255,0,224,156,159,240,90,223,28,216,90,253,171,246,119,253,179,96,178,88,18,59,59,63,19,248,19,226,126,149,5,181,177,85,64,131,77,215,153,231,179,12,138,190,101,187,233,209,72,161,21,36,136,99,106,124,206,55,51,194,224,228,233,60,155,136,241,74,45,197,253,91,39,206,177,23,123,217,79,15,151,170,115,91,165,40,213,146,182,169,171,169,30,222,25,194,170,246,159,219,89,13,9,75,190,39,46,166,247,221,170,216,185,74,13,239,239,65,61,116,181,253,219,214,223,240,67,63,248,41,142,178,226,231,196,95,1,126,51,105,173,57,103,187,223,240,215,226,125,244,225,221,139,177,88,244,159,2,50,49,44,75,49,251,81,44,196,146,220,138,240,107,241,117,106,23,88,78,7,226,202,237,105,6,248,115,48,167,22,173,173,229,93,74,87,125,19,166,188,173,119,203,233,67,7,131,170,212,171,241,102,81,7,111,126,49,206,112,11,209,71,216,184,164,187,251,242,249,94,242,245,239,248,113,127,237,201,255,0,68,159,227,183,254,25,143,140,159,252,207,86,95,235,174,117,255,0,68,15,17,127,225,147,23,255,0,204,130,254,206,201,255,0,232,169,203,127,240,249,67,255,0,151,159,255,217};

How do I convert this c# byte[] to a java byte[] and after that convert this array to a bitmap?

p.s. in c# this way works

        File.WriteAllBytes("Foo.jpeg", bytearray ); 

p.s. the resulting byte array is not an array of bytes byte array Java .. Java -128 ... 127 and I get a lot of 0 ... 255

Stupid question ? Why minus ? If it is easy to give advice better decision !

online Thomas
  • 8,864
  • 6
  • 44
  • 85
DuosDuo
  • 153
  • 1
  • 10

2 Answers2

2
Bitmap bitmap = BitmapFactory.decodeByteArray(yourArray, 0,
yourArray.length);

I found this answer on http://developer.android.com/reference/android/graphics/BitmapFactory.html

decodeByteArray(byte[] data, int offset, int length)

Decode an immutable bitmap from the specified byte array.

  • the first parameter is obviously your array with data.
  • the second one is the offset to where the data is stored. This has nothing to do with offset you normally think of talking about images. It indicates where the data starts. read more about it on https://en.wikipedia.org/wiki/BMP_file_format
  • the third is just the length of the array

For the difference in a C# byte and a java byte:

Could you try

ByteBuffer bb = ByteBuffer.wrap(yourArray);
bb.order( ByteOrder.LITTLE_ENDIAN);
byte[] convertedArray = bb.array();

I found this helpful answer on different results when converting int to byte array - .NET vs Java

Community
  • 1
  • 1
online Thomas
  • 8,864
  • 6
  • 44
  • 85
  • the resulting byte array is not an array of bytes byte array Java .. in Java -128 ... 127 and I get a this 0 ... 255 – DuosDuo Oct 16 '15 at 07:49
  • @llnar I edited my post to also answer that part. If it doesn't work please read more about the endian difference between bytes in c# and Java and try to fix that part. About the downvoting: I think they'd like you to use full sentences, you might try to change your question to a clear, readable story. – online Thomas Oct 16 '15 at 08:04
1

I found a solution:

    public byte[] IntArrayToByteArray(int[] Iarr){
       byte[] bytes = new byte[Iarr.length];
       for (int i = 0; i < Iarr.length; i++) {
         bytes[i] = (byte)  (Iarr[i] & 0xFF);
       }
      return bytes;
    }
DuosDuo
  • 153
  • 1
  • 10
  • Well that is exactly what endian's do: https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html >Why is endianness so important? Suppose you are storing int values to a file, then you send the file to a machine which uses the opposite endianness and read in the value. You'll run into problems because of endianness. You'll read in reversed values that won't make sense. – online Thomas Oct 16 '15 at 09:31