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读写是报错,感谢网友的分享
在fragment切换时传数据
最近在项目中,需要两个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(); }
相关文章推荐
- android studio 的 cannot resolve symbol R
- Android集成Mina NIO Socket
- Android(java)同步方法synchronized
- 触摸控制布局移动和缩放
- android源码解析之(十)-->Launcher启动流程
- Android Studio 自定义方法注释模板
- Android开发:SharedPreferences 存储数据、获取数据
- android之notifyDataSetChanged失效
- 【转】Android中自定义控件的步骤
- 【转】Android自定义控件
- Android长时间及连续两次按BackUp按键退出程序
- [置顶] Android Studio 中使用Lambda
- Android Hugo
- 开源框架android-times-square实现日历选择日期
- 【Android基础】学生管理系统
- Android Studio之基本设置与运行
- Android中内存优化
- Android Studio调试功能使用总结
- Android ServiceConnection详解
- Android设置Dialog是否可隐藏