hashmap - difference between two way of converting JSON byteArray -
i have json image response this:
"userimage":[ 255, 216, 255, 224, 0,.....]
there 2 way make response byte array:
1 -
jsonarray resultimage = result.getjsonarray("userimage"); byte[] byteuserimage = resultimage.tostring().getbytes(); hashuserimagemap.put(userid, byteuserimage);
2-
byte[] tmp=new byte[result.getjsonarray("userimage").length()]; for(int i=0;i<result.getjsonarray("userimage").length();i++){ tmp[i]=(byte)(((int)result.getjsonarray("userimage").get(i)) & 0xff); } hashuserimagemap.put(userid, tmp);
in second way convert bytearray bitmap:
byte[] arr = getmapinstance().get(name); bitmap bitmap = bitmapfactory.decodebytearray(arr, 0, arr.length);
but in first way bitmap null. want know deference between these tow way?
the first method calls resultimage.tostring.getbytes
. create json string , give ascii values each of characters contained in there.
for "[42]"
these bytes: [0x5b, 0x34, 0x32, 0x5d]
. wrong , there many of them. bitmapfactory
reject it.
the second method goes through array element element, treats number found there byte value , constructs new array of value.
for "[42]"
[0x2a]
(which want).
Comments
Post a Comment