Passing the Image in PutExtra in Android

2019-04-01 00:40发布

问题:

I want to pass the Background Image that I have set to Button in PutExtra() with intent object into another Class.

Can anybody know how to do that ?

Thanks davidbrown

回答1:

Sender Activity:

Bitmap bitmap = BitmapFactory.decodeResource
                (getResources(), R.drawable.sticky_notes); // your bitmap
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bs);
        intent.putExtra("byteArray", bs.toByteArray());

Reciever Activity:

 if(getIntent().hasExtra("byteArray")) {
            ImageView imv= new ImageView(this);
            Bitmap bitmap = BitmapFactory.decodeByteArray(
                    getIntent().getByteArrayExtra("byteArray"), 0, getIntent().getByteArrayExtra("byteArray").length);
            imv.setImageBitmap(bitmap);
          }


回答2:

Intent can keep only 40 kbytes. If you can zip your images less then 40 kbytes - you can put it into extras



回答3:

intent.putExtra("imageData", bitmap)

better approach is to create a link instead of passing directly bitmap.

intent.putExtra("image_url",R.drawable.image);


回答4:

try this...

first get image in bitmap.

Bitmap tileImage = BitmapFactory.decodeResource(getResources(), R.drawable.floore);

Conver it in byte array.

 ByteArrayOutputStream stream = new ByteArrayOutputStream();
      photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
      byte[] byteArray = stream.toByteArray();

      Bundle b = new Bundle();
      b.putByteArray("camara",byteArray);

      Intent intent3 = new Intent(this,Second.class);
      intent3.putExtras(b);
      startActivity(intent3);          


回答5:

You can pass Bitmap (since it's implementing Parcelable) if you're sure that it won't be deleted from memory (in other words - don't store Bitmaps like that).

Bitmap itself is just a small Java wrapper of native resources, so it won't take a lot of space.