简体   繁体   English

如何使用 Parcelable 传递 Drawable

[英]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.

相关问题 如何使用 android 中的 Parcelable 接口通过意图将包含可绘制参数的 object 传递给另一个活动 - how to pass an object containing with drawable parameter to another activity through intent using Parcelable Interface In android 如何使用parcelable传递BitmapShader类对象? - how to pass BitmapShader class object using parcelable? 如何在Android中使用Parcelable传递字符串数组 - How to pass string array using parcelable in android 如何使用宗地将CountDownTimer对象传递给Intent? - How to pass object of CountDownTimer into intent using parcelable? 使用可拆分的Intent传递数组 - Pass array in Intent using parcelable 如何使用Parcelable在活动之间传递对象的ArrayList? - How to pass ArrayList of object between activities using Parcelable? 如何使用Parcelable在活动之间传递带有其他对象列表的对象? - How to pass object with List of other object between activities using Parcelable? 如何使用parcelable在活动之间传递线程类对象 - how to pass a thread class object from activity to activity using parcelable 如何使用Parcelable传递带有2D ArrayList字段的自定义对象 - How to pass a custom object with a 2D ArrayList field using Parcelable 如何在 android studio 中使用 parcelable 在活动之间传递数据 - How to pass data between activity using parcelable in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM