How can i find binder object in a android parcel? i want to marshall a parcel which has got a bitmap but i got Runtime exception says that the parcel has got binder object.
1 回答
Sadly, you can't. A Parcel
is unfortunately sometimes more than just a stream of bytes. Occasionally it contains an intelligent object, called a Binder. These objects can be passed around using IPC and have methods called by different processes in different parts of the Android system.
That's what's happening in this case. When you called Bitmap.writeToParcel()
it's putting some intelligent object in there, which needs to be queried by other parts of the OS. That means that this Parcel simply can't be reduced to a stream of bytes.
(Specifically I think what's happening is this - but I could be wrong. I believe that this code: http://androidxref.com/4.1.1/xref/frameworks/native/libs/binder/Parcel.cpp#736 is writing the bitmap data to an area of shared memory, and putting a reference to that shared memory area into the parcel. This means that the data doesn't need to be copied so often, which is great when you're passing the Parcel to another process using IPC, but not so good if you're just using it to serialize data.)
Using Parcel.marshall
sometimes suggests bad design... as the comment says,
The data you retrieve here must not be placed in any kind of persistent storage (on local disk, across a network, etc). For that, you should use standard serialization or another kind of general serialization mechanism. The Parcel marshalled representation is highly optimized for local IPC, and as such does not attempt to maintain compatibility with data created in different versions of the platform.
If you are using it for local IPC, then you shouldn't need to call Parcel.marshall
- because normally it would be part of an AIDL interface where these things are handled automatically.
Sorry there's no immediate solution! If you're using it for IPC, then use AIDL. If you're using it for something else, then don't use Parcel.marshall
- instead you'll have to go to more effort to write the bitmap bytes to your own data format.