pass a bitmap image from one activity to another

后端 未结 8 1554
清酒与你
清酒与你 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:08

    There are 3 Solutions to solve this issue.

    1) First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView.

    Convert Bitmap to Byte Array:-

    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    

    Pass byte array into intent:-

    Intent intent = new Intent(this, NextActivity.class);
    intent.putExtra("picture", byteArray);
    startActivity(intent);
    

    Get Byte Array from Bundle and Convert into Bitmap Image:-

    Bundle extras = getIntent().getExtras();
    byte[] byteArray = extras.getByteArray("picture");
    
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    ImageView image = (ImageView) findViewById(R.id.imageView1);
    
    image.setImageBitmap(bmp);
    

    2) First Save image into SDCard and in next activity set this image into ImageView.

    3) Pass Bitmap into Intent and get bitmap in next activity from bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity.

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

    I've found the easiest (but definitely not the most elegant) way is to use a static class member. eg:

    class PassedData
    {
        public Bitmap bm1, bm2, etc;
    
        private PassedData current;
    
        public static PassedData getCurrent() {return current;}
    
        public PassedData()
        {
            current = this;
        }
    }
    

    Then each activity can reference PassedData.getCurrent().

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

    IGP summed it up clearly, but in my opinion the most efficient way to do it is by passing the URI of the image to the next activity, instead of the Bitmap itself. I'm actually not sure if it's possible to pass whole Bitmaps (or, converted, ByteArrays) of data from one activity to another using Intents - I believe there is a limit as to how much data a Bundle can contain.

    Instead pass the reference you're using to display the image in the first activity. I assume you're using some kind of lazy loading? If not, I highly suggest you do. This way you can simply re-query for the Bitmap via the URI.

    However, I am able to get the images from gallery but as soon as I select one the application crashes

    I'm still puzzled as to how these kinds of problems reach SO. Check the logs, maybe you can figure it out on your own.

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

    Since you are retrieving the image from the Gallery, why not pass the id to the next activity and retrieve the image in that activity rather than passing the image? This will help you on memory and performance.

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

    I think you can do by define your Bitmap as static and by calling classname.bitmap you can get the bitmap..and set as background in next Activity

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

    Bitmap implements Parcelable, so you could always pass it in the intent. try below code:

    First Activity.

    Intent mIntent = new Intent(this, ActivityTwo.class);
    mIntent.putExtra("bmp_img", bmp);
    

    for getting output in target activity, try:

    Second Activity.

    Bitmap mBitmap = (Bitmap) intent.getParcelableExtra("bmp_img");
    

    you always use getParcelableExtra("key") for get the passing Bitmap in Activity.

    0 讨论(0)
提交回复
热议问题