Android Bitmap Serialization

Source: Internet
Author: User

Recently in the development of the need to use the image bitmap serialization and delivery, found that bitmap is not serialized, the following is the implementation of a serialization method, sharing.
The following is serialized by Byte[], because bitmap is not serialized, we need to implement it, you can choose to inherit bitmap to implement serialization, where byte[] mode is used.
You can then use Intent's Putextra (String name,parcelable value) to send the bitmap.

ImportJava.io.ByteArrayOutputStream;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Bitmap.CompressFormat;ImportAndroid.os.Parcel;Importandroid.os.Parcelable;/***@authortcwkid* @date 2011-12-07*/ Public classDetailiconImplementsparcelable{PrivateBitmap Draw;Private Static byte[] bytedraw; PublicBitmap Getdraw () {returnDraw;}  Public voidSetdraw (Bitmap draw) { This. Draw =Draw;} @Override Public intdescribecontents () {return0;} @Override Public voidWritetoparcel (Parcel dest,intflags) {Bytedraw=getBytes (Draw);d Est.writebytearray (Bytedraw);//In fact, the writing here is sequential, first-written first read}  Public Static FinalParcelable.creator creator=NewCreator () {@Override PublicDetailicon[] NewArray (intsize) {return Newdetailicon[size];} @Override PublicDetailicon Createfromparcel (Parcel source) {Detailicon di=NewDetailicon (); Source.readbytearray (Bytedraw);d I.setdraw (Getbitmap (Bytedraw));returndi;}}; Private StaticBitmap Getbitmap (byte[] data) {returnBitmapfactory.decodebytearray (data, 0, data.length);} Private byte[] getBytes (Bitmap Bitmap) {bytearrayoutputstream baops=NewBytearrayoutputstream (); Bitmap.compress (Compressformat.png,0, baops);returnBaops.tobytearray ();}}

Android Bitmap Serialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.