花了一段时间,但我找到了解决此问题的干净方法。我制作了一个自定义对象(BitmapDataObject),该对象实现了Serializable,并具有byte
[]来存储原始Bitmap中的PNG数据。使用此方法,可以将数据正确存储在ObjectOutputStream / ObjectInputStream中-
通过将Pmap存储为PNG到自定义对象的byte []中,可以有效地允许序列化和反序列化Bitmap对象。下面的代码解决了我的查询。
private String title;private int sourceWidth, currentWidth;private int sourceHeight, currentHeight;private Bitmap sourceImage;private Canvas sourceCanvas; private Bitmap currentImage;private Canvas currentCanvas; private Paint currentPaint;protected class BitmapDataObject implements Serializable { private static final long serialVersionUID = 111696345129311948L; public byte[] imageByteArray;}private void writeObject(ObjectOutputStream out) throws IOException{ out.writeObject(title); out.writeInt(currentWidth); out.writeInt(currentHeight); ByteArrayOutputStream stream = new ByteArrayOutputStream(); currentImage.compress(Bitmap.CompressFormat.PNG, 100, stream); BitmapDataObject bitmapDataObject = new BitmapDataObject(); bitmapDataObject.imageByteArray = stream.toByteArray(); out.writeObject(bitmapDataObject);}private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{ title = (String)in.readObject(); sourceWidth = currentWidth = in.readInt(); sourceHeight = currentHeight = in.readInt(); BitmapDataObject bitmapDataObject = (BitmapDataObject)in.readObject(); Bitmap image = BitmapFactory.depreByteArray(bitmapDataObject.imageByteArray, 0, bitmapDataObject.imageByteArray.length); sourceImage = Bitmap.createBitmap(sourceWidth, sourceHeight, Bitmap.Config.ARGB_8888); currentImage = Bitmap.createBitmap(sourceWidth, sourceHeight, Bitmap.Config.ARGB_8888); sourceCanvas = new Canvas(sourceImage); currentCanvas = new Canvas(currentImage); currentPaint = new Paint(Paint.ANTI_ALIAS_FLAG); thumbnailPaint = new Paint(Paint.ANTI_ALIAS_FLAG); thumbnailPaint.setARGB(255, 200, 200, 200); thumbnailPaint.setStyle(Paint.Style.FILL);}


