Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To simplify things, you can use standard serialization for all fields other than the bitmap. Just mark the bitmap as transient, and use out.defaultWriteObject(); and in.defaultReadObject();. This really cleans up the code:</p> <pre><code>private String title; private String description; private transient Bitmap icon; private synchronized void writeObject(final ObjectOutputStream out) throws IOException { // Serialize everything but the image out.defaultWriteObject(); // Now serialize the image out.writeInt(icon.getRowBytes()); out.writeInt(icon.getHeight()); out.writeInt(icon.getWidth()); out.writeInt(icon.getConfig().ordinal()); final int bmSize = icon.getRowBytes() * icon.getHeight(); if (dst == null || bmSize &gt; dst.capacity()) { dst = ByteBuffer.allocate(bmSize); } dst.rewind(); icon.copyPixelsToBuffer(dst); dst.flip(); out.write(dst.array(), 0, bmSize); } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { // Read everything but the image in.defaultReadObject(); // Now read the image final int nbRowBytes = in.readInt(); final int height = in.readInt(); final int width = in.readInt(); final Bitmap.Config config = Bitmap.Config.values()[in.readInt()]; final int bmSize = nbRowBytes * height; if (dst == null || bmSize &gt; dst.capacity()) { dst = ByteBuffer.allocate(bmSize); } dst.rewind(); in.read(dst.array(), 0, bmSize); icon = Bitmap.createBitmap(width, height, config); icon.copyPixelsFromBuffer(dst); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload