get ImageView's image and send it to an activity with Intent

前端 未结 7 1527
孤街浪徒
孤街浪徒 2020-12-06 03:22

I have a grid of many products in my app. when the user selects one of the item in the grid, I am starting a new activity as DIALOG box and display the item\'s name,quantity

7条回答
  •  借酒劲吻你
    2020-12-06 04:10

    You can convert your drawable to bitmap then into byte array and pass this byte array with intent.

    Drawable d;  
    Bitmap b= ((BitmapDrawable)d).getBitmap();
    
    int bytes = b.getByteCount();
    ByteBuffer buffer = ByteBuffer.allocate(bytes);  
    b.copyPixelsToBuffer(buffer);  
    byte[] array = buffer.array();
    

    user this code :

    calling activity...

    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);
    

    ...and in your receiving activity

    if(getIntent().hasExtra("byteArray")) {
        ImageView previewThumbnail = new ImageView(this);
        Bitmap b = BitmapFactory.decodeByteArray(
            getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length); 
    }
    

提交回复
热议问题