2013年5月14日
[JAVA] 傳送object發生 java.io.StreamCorruptedException
private byte[] objtobytearray(IPinfo gotobj, byte[] key) {
//gotobj是一個IPinfo object
  ByteArrayOutputStream buffers = new ByteArrayOutputStream();
  try {
   ObjectOutputStream out = new ObjectOutputStream(buffers);
   out.writeObject(gotobj);
   out.flush();
   out.close();
   //out close之前要加flush
  } catch (Exception e) {
   System.out.println("error");
  }
                byte[] guarddataarray = new byte[buffers.toByteArray().length];
  System.out.println("the data length length:"+guarddataarray.length);
  System.out.println("the key length length:"+key.length);
  if (guardflag)
   guarddataarray = guard.AES_Data_Encrypter(buffers.toByteArray(),
     key);
  else
   guarddataarray = buffers.toByteArray();
  return guarddataarray;
 }
如果沒有在 out.close之前加入 flush
則透過socket傳送buffers的資料時
接收方可能會發生 java.io.StreamCorruptedException
上網查詢原因時,因為若沒有加入flush,可能buffers內沒有結束符號,導致接收方,認為stream沒有結束
所以會產生exception
2013年5月13日
Drawable bitmap byte陣列互相轉換
Drawable轉bitmap
Drawable oDrawable = xxx; //xxx根據自己的情況獲取drawable BitmapDrawable BD = (BitmapDrawable) oDrawable; Bitmap BM = BD.getBitmap();Bitmap轉byte陣列
Bitmap newbm; // 把 bitmap 轉成 byte ByteArrayOutputStream stream = new ByteArrayOutputStream(); newbm.compress(Bitmap.CompressFormat.JPEG, 100, stream ); byte bytes[] = stream.toByteArray();byte陣列轉Bitmap
Bitmap geticon byte[] data geticon = BitmapFactory.decodeByteArray(data, 0, len);Bitmap轉Drawable
Drawable showicon = new BitmapDrawable(geticon);
訂閱:
意見 (Atom)