私はDrawable
メンバーとしているクラスを持っています。
このクラスは、アクティビティ間でデータを追加で送信するために使用していますParcelable
。
そのために、パーセブルを拡張し、必要な機能を実装しました。
読み取り/書き込みの int/string を使用して、基本的なデータ型を送信できます。
しかし、Drawable オブジェクトのマーシャリング中に問題に直面しています。
Drawable
そのために、をに変換しようとしましたbyte array
が、クラス キャスト例外が発生しています。
次のコードを使用して、Drawable を Byte 配列に変換しています。
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);
そして、バイト配列を Drawable に変換するために、次のコードを使用しています:
final int contentBytesLen = in.readInt();
byte[] contentBytes = new byte[contentBytesLen];
in.readByteArray(contentBytes);
mMyDrawable = new BitmapDrawable(BitmapFactory.decodeByteArray(contentBytes, 0, contentBytes.length));
これを実行すると、クラスキャスト例外が発生します。
HashMap を使用して Drawable を作成/渡すにはどうすればよいですか?
Drawable in Parcel を渡す方法はありますか。
ありがとう。