如何:在 Android 中打包位图
我有一个序列化类,我想向其中添加位图,但位图不支持序列化。
相反,我想我应该使用包裹,但无法让它工作。
这是一些使用局部变量的测试代码:
Parcel parcel;
Bitmap sourceBitmap;
Bitmap destinationBitmap;
parcel = Parcel.obtain();
sourceBitmap = Bitmap.createBitmap(200, 400, Config.ARGB_8888);
sourceBitmap.writeToParcel(parcel, 0);
destinationBitmap = Bitmap.CREATOR.createFromParcel(parcel);
我在上面的最后一行收到以下错误:
09-06 21:18:20.463: DEBUG/skia(17716): Bitmap_createFromParcel unknown config: 0
09-06 21:18:20.473: DEBUG/AndroidRuntime(17716): Shutting down VM
09-06 21:18:20.483: WARN/dalvikvm(17716): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
09-06 21:18:20.493: ERROR/AndroidRuntime(17716): Uncaught handler: thread main exiting due to uncaught exception
09-06 21:18:20.513: ERROR/AndroidRuntime(17716): java.lang.RuntimeException: Failed to unparcel Bitmap
09-06 21:18:20.513: ERROR/AndroidRuntime(17716): at android.graphics.Bitmap$1.createFromParcel(Bitmap.java:899)
09-06 21:18:20.513: ERROR/AndroidRuntime(17716): at android.graphics.Bitmap$1.createFromParcel(Bitmap.java:903)
I have a serialized class which I want to add a bitmap to, but Bitmap doesn't support serialize.
Instead I thought I'd use a parcel instead, but can't get it to work.
Here's some test code using local variables:
Parcel parcel;
Bitmap sourceBitmap;
Bitmap destinationBitmap;
parcel = Parcel.obtain();
sourceBitmap = Bitmap.createBitmap(200, 400, Config.ARGB_8888);
sourceBitmap.writeToParcel(parcel, 0);
destinationBitmap = Bitmap.CREATOR.createFromParcel(parcel);
I get the following error on the last line above:
09-06 21:18:20.463: DEBUG/skia(17716): Bitmap_createFromParcel unknown config: 0
09-06 21:18:20.473: DEBUG/AndroidRuntime(17716): Shutting down VM
09-06 21:18:20.483: WARN/dalvikvm(17716): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
09-06 21:18:20.493: ERROR/AndroidRuntime(17716): Uncaught handler: thread main exiting due to uncaught exception
09-06 21:18:20.513: ERROR/AndroidRuntime(17716): java.lang.RuntimeException: Failed to unparcel Bitmap
09-06 21:18:20.513: ERROR/AndroidRuntime(17716): at android.graphics.Bitmap$1.createFromParcel(Bitmap.java:899)
09-06 21:18:20.513: ERROR/AndroidRuntime(17716): at android.graphics.Bitmap$1.createFromParcel(Bitmap.java:903)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须重置您的包裹:
you have to reset your parcel:
Bitmap 已经由 android
http://developer .android.com/reference/android/graphics/Bitmap.html#writeToParcel(android.os.Parcel, int)
Bitmap has already pacelled by android
http://developer.android.com/reference/android/graphics/Bitmap.html#writeToParcel(android.os.Parcel, int)