[英]How to Pass Drawable using Parcelable
I have a class where i have an Drawable
as an member.我有一个 class,其中我有一个
Drawable
作为成员。
This class i am using for sending data across activities as an Parcelable
extra.我正在使用这个 class 作为
Parcelable
extra 跨活动发送数据。
For that i have extended the parceble, and implemented the required functions.为此,我扩展了 parceble,并实现了所需的功能。
I am able to send the basic data types using read/write int/string.我能够使用读/写 int/string 发送基本数据类型。
But i am facing problem while marshaling the Drawable object.但是我在编组 Drawable object 时遇到问题。
For that i tried to convert the Drawable
to byte array
, but i am getting class cast Exceptions.为此,我尝试将
Drawable
转换为byte array
,但我收到 class 强制转换异常。
I am using following code to covert my Drawable to Byte array:我正在使用以下代码将我的 Drawable 转换为字节数组:
Bitmap bitmap = (Bitmap)((BitmapDrawable) mMyDrawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[]byteArray = stream.toByteArray();
out.writeInt(byteArray.length);
out.writeByteArray(byteArray);
And to convert byte array to Drawable i am using following code:并将字节数组转换为 Drawable 我正在使用以下代码:
final int contentBytesLen = in.readInt();
byte[] contentBytes = new byte[contentBytesLen];
in.readByteArray(contentBytes);
mMyDrawable = new BitmapDrawable(BitmapFactory.decodeByteArray(contentBytes, 0, contentBytes.length));
When i run this i get Class cast exception.当我运行它时,我得到 Class 转换异常。
How can we write/pass Drawable using the HashMap?我们如何使用 HashMap 写入/传递 Drawable?
Is there any way by which we can pass Drawable in Parcel.有什么方法可以在 Parcel 中传递 Drawable。
Thanks.谢谢。
As you already convert Drawable to Bitmap in your code, why not use Bitmap as an member of your Parcelable class.由于您已经在代码中将 Drawable 转换为 Bitmap,为什么不使用 Bitmap 作为 Parcelable class 的成员。
Bitmap implements Parcelable by default in API, by using Bitmap, you don't need to do anything special in your code and it will automatically handled by Parcel. Bitmap在 API 中默认实现了 Parcelable,通过使用 Bitmap,您无需在代码中做任何特殊操作,它会自动由 Parcel 处理。
Or if you insist to use Drawable, implement your Parcelable as something like this:或者,如果您坚持使用 Drawable,请将您的 Parcelable 实现为如下所示:
public void writeToParcel(Parcel out, int flags) {
... ...
// Convert Drawable to Bitmap first:
Bitmap bitmap = (Bitmap)((BitmapDrawable) mMyDrawable).getBitmap();
// Serialize bitmap as Parcelable:
out.writeParcelable(bitmap, flags);
... ...
}
private Guide(Parcel in) {
... ...
// Deserialize Parcelable and cast to Bitmap first:
Bitmap bitmap = (Bitmap)in.readParcelable(getClass().getClassLoader());
// Convert Bitmap to Drawable:
mMyDrawable = new BitmapDrawable(bitmap);
... ...
}
Hope this helps.希望这可以帮助。
In my app, I saved Drawable/BitMap into Cache and passing it using file's path String instead.在我的应用程序中,我将 Drawable/BitMap 保存到缓存中并使用文件路径字符串传递它。
Not a solution you were looking for, but at least some alternative for your problem.不是您正在寻找的解决方案,但至少是您问题的一些替代方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.