【发布时间】:2012-06-01 23:19:09
【问题描述】:
我有一个实现 Serializable 的类 TouchPoint,因为它包含位图,所以我为该类编写了 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);
}
}
问题是我得到了
SkImageDecoder::Factory 返回 null
那么我该如何解决它。我知道可能的解决方案是将 writeObject() 更改为
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
_bmp.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
oos.writeObject(byteStream.toByteArray);
但是这种方法慢了将近 10 倍以上。
- copyPixelsToBuffer ~14ms 用于写入图像
- _bmp.compress ~ 160ms
更新 发现实际问题是在
之后buffer.array();
所有byte[]数组元素为:0
【问题讨论】:
-
您没有收到任何其他错误消息吗?也许,
int bytes = _bmp.getRowBytes() * _bmp.getHeight()可以解决您的问题。 -
不,我没有收到其他消息。这并不能解决问题。但是,我找到了解决这个问题的方法。我稍后会发布答案。
标签: java android bitmap bytearray serializable