Serializable和Parcelable
Serializable(接口)
通過intent?的bundle傳遞參數
Bundle?bundle?=?new?Bundle();
?????bundle.putSerializable(IntentKeys.IMG_ARR_ENVIR_IMG,?mArrListEnvir_img);
?????intent.putExtras(bundle);
mListEnvir?=?(ArrayList<EnvirImg>)?(bundle
????.getSerializable(IntentKeys.IMG_ARR_ENVIR_IMG));
?
Parcelable(接口)
通過intent?的bundle傳遞參數
Bundle?bundle?=?new?Bundle();
bundle.putParcelable(IntentKeys.IMG_ARR_ENVIR_IMG,?mArrListEnvir_img);
intent.putExtras(bundle);
?
import?android.graphics.Bitmap;
import?android.os.Parcel;
import?android.os.Parcelable;
public?class?UploadPic?implements?Parcelable{
?private?String?id;
?private?String?sdPath;
?private?String?webPath;
?private?Bitmap?bmp;
?public?String?getId()?{
??return?id;
?}
?public?void?setId(String?id)?{
??this.id?=?id;
?}
?public?String?getSdPath()?{
??return?sdPath;
?}
?public?void?setSdPath(String?sdPath)?{
??this.sdPath?=?sdPath;
?}
?public?String?getWebPath()?{
??return?webPath;
?}
?public?void?setWebPath(String?webPath)?{
??this.webPath?=?webPath;
?}
?public?Bitmap?getBmp()?{
??return?bmp;
?}
?public?void?setBmp(Bitmap?bmp)?{
??this.bmp?=?bmp;
?}
?@Override
?public?int?describeContents()?{
??return?0;
?}
?@Override
?public?void?writeToParcel(Parcel?parcel,?int?flags)?{
??parcel.writeString(id);????
??parcel.writeString(sdPath);????
??parcel.writeString(webPath);????
??bmp.writeToParcel(parcel,?0);
?}
????public?static?final?Parcelable.Creator<UploadPic>?CREATOR?=?new?Creator<UploadPic>()?{?
???????????public?UploadPic?createFromParcel(Parcel?source)?{?
????????????UploadPic?pic?=?new?UploadPic();?
????????????pic.id?=??source.readString();
????????????pic.sdPath?=?source.readString();
????????????pic.webPath?=?source.readString();?//傳遞string數據
????????????pic.bmp?=?Bitmap.CREATOR.createFromParcel(source);//傳遞bitmap數據
?????????????return?pic;?
???????????}?
???????????public?UploadPic[]?newArray(int?size)?{?
???????????????return?new?UploadPic[size];?
???????????}?
???????};?
}
?
Parcelable比Serializable效率高
parcelable也不適合傳輸大量圖片數據
android.os.TransactionTooLargeException?不適合傳大量數據尤其bitmap?intent跳轉的時候無縫傳遞數據pacicl
導致原因是:Binder傳輸的數據太大
如果Binder的參數或返回值太大,不適合的事務緩沖區,然后調用將失敗,并將被拋出TransactionTooLargeException。
解決方法:
不要將大量數據傳入Binder
?
轉載于:https://blog.51cto.com/lyp634959848/1335606
總結
以上是生活随笔為你收集整理的Serializable和Parcelable的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Git 远程分支的查看及相关问题
- 下一篇: 如何用outlook express 收