我有一个级触摸点, 用于执行可序列化, 并且因为它包含 Bitmap 我写了写Object and readObject for that class:
private void writeObject(ObjectOutputStream oos) throws IOException {
long t1 = System.currentTimeMillis();
oos.defaultWriteObject();
if(_bmp!=null){
int bytes = _bmp.getWidth()*_bmp.getHeight()*4;
ByteBuffer buffer = ByteBuffer.allocate(bytes);
_bmp.copyPixelsToBuffer(buffer);
byte[] array = buffer.array();
oos.writeObject(array);
}
Log.v("PaintFX","Elapsed Time: "+(System.currentTimeMillis()-t1));
}
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException{
ois.defaultReadObject();
byte[] data = (byte[]) ois.readObject();
if(data != null && data.length > 0){
_bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
}
}
问题是,我得到
SkimageDecoder :: factory 返回无效
如何修补它。 我知道可能的解决方案是 将写对象() 更改为
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
_bmp.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
oos.writeObject(byteStream.toByteArray);
但这种方法慢了近10倍以上。
- copyPixelsToBuffer ~14ms for writing image
- _bmp.compress ~ 160ms
UPDATE Find out that the actual problem is that after
buffer.array();
所有字节数组元素为: 0