我有一个实现了Serializable接口的TouchPoint类,因为它包含Bitmap,所以我为该类编写了writeObject和readObject方法:
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);
}
}
问题是我遇到了以下错误:
那么我该如何解决呢?我知道可能的解决方案是将 writeObject() 改为:SkImageDecoder::Factory 返回 null
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
_bmp.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
oos.writeObject(byteStream.toByteArray);
但是这种方法几乎慢了10倍以上。
- copyPixelsToBuffer 写入图像大约需要14毫秒
- _bmp.compress 大约需要160毫秒
更新发现实际问题是在此之后。
buffer.array();
所有 byte[] 数组元素都是:0。
int bytes = _bmp.getRowBytes() * _bmp.getHeight()
可以解决你的问题。 - a.ch.