How to send the bitmap into bundle

后端 未结 5 974
北荒
北荒 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:20

    If you want to pass image using bundle then i am sure it will help you.

    Bundle bundle = new Bundle();
    bundle.putParcelable("bitmap", bitmap);
    fragment.setArguments(bundle);
    
    0 讨论(0)
  • 2021-02-02 16:26

    No need to convert bitmap to byte array. You can directly put bitmap into bundle. Refer following code to put bitmap into bundle.

    bundle.putParcelable("BitmapImage",bitmapname);
    

    Get bitmap from Bundle by following code

    Bitmap bitmapimage = getIntent().getExtras().getParcelable("BitmapImage");
    
    0 讨论(0)
  • 2021-02-02 16:29

    I think it is easier to send the path or address of the image as a string and load it on the other side.

    If the image is a web address, you can use Glide or Picasso libraries and cache them, so on the other activities or fragments it will not load twice.

    0 讨论(0)
  • 2021-02-02 16:32

    if you are using NavigationComponent, you should use safeArgs !

    you can put arguments in nav_graph like this :

    <argument
            android:name="profileImage"
            app:nullable="true"
            app:argType="android.graphics.Bitmap" />
    

    and send it like it : (First Fragment)

    findNavController().navigate(SettingFragmentDirections.actionSettingFragmentToHomeFragment(bitmap))
    

    and give it like this : (Second Fragment)

     val bitmapimage =
                arguments?.getParcelable<Bitmap>("profileImage")
    
    
            user_profile_img.setImageBitmap(bitmapimage)
    

    read more : https://developer.android.com/guide/navigation/navigation-pass-data

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题