4

アプリに多くの製品のグリッドがあります。ユーザーがグリッド内のアイテムの 1 つを選択すると、DIALOG ボックスとして新しいアクティビティを開始し、アイテムの名前、数量、画像を表示します。しかし、画像ソースを動的に送信することはできません。

ここに私のコードがあります

gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
            //Toast.makeText(getApplicationContext(),((TextView) v.findViewById(R.id.grid_item_label)).getText(), Toast.LENGTH_SHORT).show();
            Intent item_intent = new Intent(MainActivity.this, Item.class);
            item_intent.putExtra("name",((TextView) v.findViewById(R.id.grid_item_label)).getText());
            item_intent.putExtra("quantity",((TextView) v.findViewById(R.id.grid_item_quantity)).getText());

            //my problem is here***************************************************
            ImageView my_image =  findViewById(R.id.grid_item_image).getDrawable();
            item_intent.putExtra("image",my_image.getXXXXX());
            //*********************************************************************
            MainActivity.this.startActivity(item_intent);

        }
    });

ImageView から画像ソースを取得するには、何を使用すればよいですか?

4

7 に答える 7

35

バンドルを次のように使用します

imageView.buildDrawingCache();
Bitmap image= imageView.getDrawingCache();

 Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intent.putExtras(extras);
startActivity(intent);


Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");

image.setImageBitmap(bmp );
于 2013-07-26T10:37:50.917 に答える
2

Parcable (ビットマップなど) またはシリアライズ可能なオブジェクトをバンドルに配置し、他のアクティビティでバンドルからオブジェクトを取得できます。

バンドルを使用して Android アクティビティ間で画像 (ビットマップ) を渡すにはどうすればよいですか?

アクティビティ間で大量のデータを渡すため、お勧めしませんが。画像参照 ID を次のアクティビティに渡し、そこで画像を取得することをお勧めします。

selectedListItem のオブジェクトを別のアクティビティに渡す方法は?

編集:

intent.putExtra("imageId", imageId);

他のアクティビティで:

int imageRef = getIntent().getIntExtra("imageId", defaultValue);

http://developer.android.com/reference/android/content/Intent.html#getIntExtra%28java.lang.String,%20int%29

于 2013-07-26T10:33:34.070 に答える
2

このソリューションを使用して、ドローアブルを Bitmap に変換する必要があります。Bitmap クラスは Parcelable インターフェイスを実装しているため、インテントを介して次のアクティビティに渡すことができます。

于 2013-07-26T10:34:52.897 に答える
1

ドローアブルをビットマップに変換してからバイト配列に変換し、このバイト配列を意図的に渡すことができます。

Drawable d;  
Bitmap b= ((BitmapDrawable)d).getBitmap();

int bytes = b.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);  
b.copyPixelsToBuffer(buffer);  
byte[] array = buffer.array();

このコードを使用してください:

呼び出しアクティビティ...

Intent i = new Intent(this, NextActivity.class);
Bitmap b; // your bitmap
ByteArrayOutputStream bs = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("byteArray", bs.toByteArray());
startActivity(i);

...そして受信アクティビティで

if(getIntent().hasExtra("byteArray")) {
    ImageView previewThumbnail = new ImageView(this);
    Bitmap b = BitmapFactory.decodeByteArray(
        getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length); 
}
于 2013-07-26T10:35:29.823 に答える