我有一個類接觸點,其實現Serializable因爲其包含的位圖我寫的writeObject和readObject該類:decodeByteArray和copyPixelsToBuffer不工作。 SkImageDecoder ::廠返回null
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 ::廠返回null
所以,我怎麼能解決這個問題。我知道,可能的解決辦法是改變writeObject()將
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
_bmp.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
oos.writeObject(byteStream.toByteArray);
但這種方法比較慢幾乎10+倍。
寫入圖像UPDATE
- copyPixelsToBuffer〜14MS 找出實際的問題是,經過
buffer.array();
所有的byte []數組元素爲:0
你不得到任何其他錯誤消息? 也許,'int bytes = _bmp.getRowBytes()* _bmp.getHeight()'可以解決你的問題。 –
沒有,我沒有得到其他消息。這並不能解決問題。但是,我找到了解決這個問題的方法。我稍後會發布答案。 – Cyberon