3

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

David Brown
  • 4,633
  • 17
  • 47
  • 72

6 Answers6

9

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);
          }
Abhijeet Mallick
  • 1,630
  • 2
  • 14
  • 19
2

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

Yahor10
  • 2,128
  • 13
  • 13
1
intent.putExtra("imageData", bitmap)

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

intent.putExtra("image_url",R.drawable.image);
subodh
  • 5,836
  • 12
  • 45
  • 67
1

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);          
Mehul Santoki
  • 1,198
  • 1
  • 10
  • 25
0

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.

Dmitry Zaytsev
  • 22,662
  • 13
  • 89
  • 139
0

Be careful when passing files that can be very large, such as a photo or gallery file. Even if you compress it, the size may exceed putExtra's acceptable limit. I suggest sending the image link or file path for a file from the gallery. In my app I always compress my files as max as I can, but there was always one that crashs the app.

Intent intent = new Intent(getActivity(), PhotoViewActivity.class);
intent.putExtra("url", url);
//OR file path
intent.putExtra("path", path);
startActivityForResult(intent,PHOTO_VIEW_REQUEST);

In that case on PhotoViewActivity

    String url = getIntent().getStringExtra("url");
    String path = getIntent().getStringExtra("path");
    if(url != null && !url.isEmpty()){
        //Get using Picasso or other framework
    }else if(path != null && !path.isEmpty()){
        //In my case I transform in Bitmap
        //see this link for more detail : https://stackoverflow.com/questions/16804404/create-a-bitmap-drawable-from-file-path

    }else{
        //Throw exception and close activity
    }

How transform path in Bitmap: Create a Bitmap/Drawable from file path

Rafael Rocha
  • 249
  • 3
  • 4