pass a bitmap image from one activity to another

后端 未结 8 1555
清酒与你
清酒与你 2020-12-05 21:57

In my app i am displaying no.of images from gallery from where as soon as I select one image , the image should be sent to the new activity where the selected image will be

相关标签:
8条回答
  • 2020-12-05 22:27

    Using this you can pass bitmap to another activity.

    If you are using drawable than convert that drawable to bitmap first.

    Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
    

    For passing that bitmap to another activity using intent use this below code snippet.

    intent.putExtra("Bitmap", bitmap);
    

    And for fetch that bitmap intent in another activity use this

    Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
    

    Follow this Link for More Detail.

    0 讨论(0)
  • 2020-12-05 22:31

    Activity

    To pass a bitmap between Activites

    Intent intent = new Intent(this, Activity.class);
    intent.putExtra("bitmap", bitmap);
    

    And in the Activity class

    Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
    

    Fragment

    To pass a bitmap between Fragments

    SecondFragment fragment = new SecondFragment();
    Bundle bundle = new Bundle();
    bundle.putParcelable("bitmap", bitmap);
    fragment.setArguments(bundle);
    

    To receive inside the SecondFragment

    Bitmap bitmap = getArguments().getParcelable("bitmap");
    

    If you are getting failed binder transaction, this means you are exceeding the binder transaction buffer by transferring large element from one activity to another activity.

    So in that case you have to compress the bitmap as an byte's array and then uncompress it in another activity, like this

    In the FirstActivity

    Intent intent = new Intent(this, SecondActivity.class);
    
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream);
    byte[] bytes = stream.toByteArray(); 
    intent.putExtra("bitmapbytes",bytes);
    

    And in the SecondActivity

    byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    
    0 讨论(0)
提交回复
热议问题