您的位置:首页 > 移动开发 > Android开发

android之Serializable

2016-04-10 20:21 525 查看
本文主要记录一些零碎的东西

最近在项目中,需要两个fragment之间切换时传递数据,fragment见切换可以使用FragmentTransaction,传递普通数据可以使用Bundle,但是Bundle只可以传送字符类型的数据,而我要传递的是Map<String,Object>类型的数据,主要里面有Bitmap图片数据,Bundle有个putSerializable可以传递序列化的数据,所以就把map序列化一下,数据可以传过去,可以显示出来,但是我在点击进行其他操作时,报如下错:

java.lang.RuntimeException: Parcelable encountered IOException writing serializa

java.io.NotSerializableException: android.graphics.Bitmap

原来是在传递Bitmap是IO读写是报错,感谢网友的分享

http://stackoverflow.com/questions/8955034/how-to-fix-a-java-io-notserializablee

原来序列化时类似Bitmap这种都是需要处理的,附上我的序列化代码

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Map;

/**
* 序列化map供Bundle传递map使用
* Created by chenling on 2016/4/10.
*  http://stackoverflow.com/questions/8955034/how-to-fix-a-java-io-notserializableexception-android-graphics-bitmap */
public class SerializableMap implements Serializable {

private transient Map<String,Object> map;
private transient Bitmap bitmap;

public SerializableMap(Map<String, Object> map, Bitmap bitmap) {
this.map = map;
this.bitmap = bitmap;
}

public Map<String, Object> getMap() {
return map;
}

public void setMap(Map<String, Object> map) {
this.map = map;
}

public Bitmap getBitmap() {
return bitmap;
}

public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}

private void writeObject(ObjectOutputStream oos) throws IOException{
// This will serialize all fields that you did not mark with 'transient'
// (Java's default behaviour)
oos.defaultWriteObject();
// Now, manually serialize all transient fields that you want to be serialized
if(bitmap!=null){
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
boolean success = bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
if(success){
oos.writeObject(byteStream.toByteArray());
}
}
}

private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException{
// Now, all again, deserializing - in the SAME ORDER!
// All non-transient fields
ois.defaultReadObject();
// All other fields that you serialized
byte[] image = (byte[]) ois.readObject();
if(image != null && image.length > 0){
bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
}
}
}

在fragment切换时传数据
FragmentTransaction transaction = getFragmentManager().beginTransaction().replace(R.id.home_change, businessItemFragment);
transaction.addToBackStack("fragment");
Bundle bundle = new Bundle();
SerializableMap serializableMap=new SerializableMap(listems.get(pos),(Bitmap)listems.get(pos).get("shop_image"));
bundle.putSerializable("business",serializableMap);
businessItemFragment.setArguments(bundle);
transaction.commit();
另一个fragment的接收
Bundle bundle = getArguments();
if(bundle != null){
SerializableMap serializableMap = (SerializableMap) bundle.get("business");
shopmap = serializableMap.getMap();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: