How to send the bitmap into bundle

后端 未结 5 973
北荒
北荒 2021-02-02 16:11

I\'m new to android. I want to pass bitmap into Bundle. But I can\'t find any solution for it. Actually, I\'m confused. I want to display an image in a Dialog fragment. But I do

5条回答
  •  再見小時候
    2021-02-02 16:33

    First of all convert it to a Byte array before adding it to intent, send it out, and decode.

    //Convertion to byte array

      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
      byte[] byteArray = stream.toByteArray();
    
    Bundle b = new Bundle();
    b.putByteArray("image",byteArray);
    
    
      // your fragment code 
    fragment.setArguments(b);
    

    get Value via intent

    byte[] byteArray = getArgument().getByteArrayExtra("image");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    

提交回复
热议问题