如何在gridview中保存对象(从库和字符串加载的位图图像)?

时间:2015-04-02 12:01:08

标签: android gridview adapter parcelable

public class MyObject implements Parcelable{
    Bitmap image;
    String title;

   @Override
   public int describeContents() {
      return 0;
   }

我不明白如何制作parcelable  对了

@Override
public void writeToParcel(Parcel dest, int flags) {

}

我也使用ImageTextAdapter并将我的对象存储在ArrayList

1 个答案:

答案 0 :(得分:1)

将位图写入parcel:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
parcel.writeInt(byteArray.length);
parcel.writeByteArray(byteArray);

从包裹中读取位图:

int length = parcel.readInt();
byte[] byteArray = new byte[length];
parcel.readByteArray(byteArray);
image = BitmapFactory.decodeByteArray(byteArray, 0, length);