how do you pass images (bitmaps) between android activities using bundles?

后端 未结 9 1116
自闭症患者
自闭症患者 2020-11-22 09:56

Suppose I have an activity to select an image from the gallery, and retrieve it as a BitMap, just like the example: here

Now, I want to pass this BitMap to be used i

相关标签:
9条回答
  • 2020-11-22 10:41

    I had to rescale the bitmap a bit to not exceed the 1mb limit of the transaction binder. You can adapt the 400 the your screen or make it dinamic it's just meant to be an example. It works fine and the quality is nice. Its also a lot faster then saving the image and loading it after but you have the size limitation.

    public void loadNextActivity(){
        Intent confirmBMP = new Intent(this,ConfirmBMPActivity.class);
    
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Bitmap bmp = returnScaledBMP();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    
        confirmBMP.putExtra("Bitmap",bmp);
        startActivity(confirmBMP);
        finish();
    
    }
    public Bitmap returnScaledBMP(){
        Bitmap bmp=null;
        bmp = tempBitmap;
        bmp = createScaledBitmapKeepingAspectRatio(bmp,400);
        return bmp;
    

    }

    After you recover the bmp in your nextActivity with the following code:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirmBMP);
        Intent intent = getIntent();
        Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
    
    }
    

    I hope my answer was somehow helpfull. Greetings

    0 讨论(0)
  • 2020-11-22 10:44

    If you pass it as a Parcelable, you're bound to get a JAVA BINDER FAILURE error. So, the solution is this: If the bitmap is small, like, say, a thumbnail, pass it as a byte array and build the bitmap for display in the next activity. For instance:

    in your 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);        
        previewThumbnail.setImageBitmap(b);
    }
    
    0 讨论(0)
  • 2020-11-22 10:47

    I would highly recommend a different approach.

    It's possible if you REALLY want to do it, but it costs a lot of memory and is also slow. It might not work if you have an older phone and a big bitmap. You could just pass it as an extra, for example intent.putExtra("data", bitmap). A Bitmap implements Parcelable, so you can put it in an extra. Likewise, a bundle has putParcelable.

    If you want to pass it inbetween activities, I would store it in a file. That's more efficient, and less work for you. You can create private files in your data folder using MODE_PRIVATE that are not accessible to any other app.

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