重複の可能性:
アクティビティ間でArrayList<Bitmap>を渡す方法
すべてのビットマップ配列要素を別のアクティビティに渡したいのですが、配列リストを渡すことはできますが、ビットマップ配列を渡すことはできません。インテント内のビットマップ配列に特別な命令はありますか?
重複の可能性:
アクティビティ間でArrayList<Bitmap>を渡す方法
すべてのビットマップ配列要素を別のアクティビティに渡したいのですが、配列リストを渡すことはできますが、ビットマップ配列を渡すことはできません。インテント内のビットマップ配列に特別な命令はありますか?
Bitmap
extends Parcelable
、これは、次のようなリストを提供できることを意味します。
ArrayList<Bitmap> bitmapList = new ArrayList<Bitmap>();
// Poupulate list here
Intent intent = new Intent();
intent.putParcelableArrayListExtra("list", bitmapList);
Bitmap[]
次に、これを受信アクティビティでに変換できます。
Bitmap[] bitmapArray = bitmapList.toArray(new Bitmap[bitmapList.size()]);
しかし、あなたの意図にあまりにも多くのものを入れることは一般的に悪い習慣であることを覚えておいてください。データ(DB、ファイルシステム、シングルトンなど)を保存し、URIまたはIDを渡すことをお勧めします。
この問題を解決するには、以下の解決策を試してください。
1)最初に画像をバイト配列に変換してからarraylistに追加し、次にこのarraylistをIntentに渡し、次のアクティビティでバンドルからarraylistを取得し、Image(Bitmap)に変換してImageViewに設定します。
ビットマップをバイト配列に変換します:-
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
次に、このバイト配列をarraylistに追加します。
バイト配列をビットマップ画像に変換します:-
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
//オブジェクトを渡す:
intent.putExtra("MyClass", obj);
//2番目のアクティビティでオブジェクトを取得します
getIntent().getSerializableExtra("MyClass");
ビットマップ配列を静的として使用し、別のアクティビティでclassname.bitmaparrayを使用します