Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7768165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:51:04+00:00 2026-06-01T15:51:04+00:00

I have a class where i have an Drawable as an member. This class

  • 0

I have a class where i have an Drawable as an member.
This class i am using for sending data across activities as an Parcelable extra.

For that i have extended the parceble, and implemented the required functions.

I am able to send the basic data types using read/write int/string.
But i am facing problem while marshaling the Drawable object.

For that i tried to convert the Drawable to byte array, but i am getting class cast Exceptions.

I am using following code to covert my Drawable to Byte array:

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:

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.

How can we write/pass Drawable using the HashMap?
Is there any way by which we can pass Drawable in Parcel.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:51:05+00:00Added an answer on June 1, 2026 at 3:51 pm

    As you already convert Drawable to Bitmap in your code, why not use Bitmap as an member of your 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.

    Or if you insist to use Drawable, implement your Parcelable as something like this:

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class with a member function that takes a default argument. struct Class
I my project I have a class that in this class I want to
I my project I have a class that in this class I want to
I have class method that returns a list of employees that I can iterate
I have extended my class with a dialog in which I have set content
Hey so i've made a 'body' class, which inherits from sf::drawable in SFML that
I have a class that extens a listview. I need to have onitemclicklistener on
I built my own TabHost with this code: I have a problem that when
I hope someone can help with this. I have created a map app that
I have my class implements Drawable. I need to redraw it periodicaly to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.