首页 > 学院 > 开发设计 > 正文

自定义一个SerializableBitmap

2019-11-09 14:53:26
字体:
来源:转载
供稿:网友
java code?
12345678910111213141516171819202122232425262728293031public class SerializableBitmap implements Serializable {        PRivate static final long serialVersionUID = -5228835919664263905L;        private Bitmap bitmap;         public SerializableBitmap(Bitmap b) {            bitmap = b;        }         // Converts the Bitmap into a byte array for serialization        private void writeObject(ObjectOutputStream out) throws IOException {            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();            boolean success = bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteStream);            byte bitmapBytes[] = byteStream.toByteArray();            if (success)                out.write(bitmapBytes, 0, bitmapBytes.length);        }         // Deserializes a byte array representing the Bitmap and decodes it        private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();            int b;            while ((b = in.read()) != -1)                byteStream.write(b);            byte bitmapBytes[] = byteStream.toByteArray();            bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);        }         public Bitmap getBitmap() {            return this.bitmap;        }    }
类中要使用Bitmap的时候就用SerializableBitmap,设置就是new SerializableBitmap(Bitmap bitmap),取出就是SerializableBitmap.getBitmap()
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表