0

I want to click a button from Activity 1 with image to show an image for Activity 2, but it's not working.

Activity1:

Intent intent = new Intent(MainActivity.this, Main2Activity.class);
// Bundle bundle = new Bundle();
Drawable drawable=img1.getDrawable();
Bitmap bitmap= ((BitmapDrawable)drawable).getBitmap();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();

intent.putExtra("picture", b);
// String ten = edt.getText().toString();
// bundle.putString("tenkh", ten);
// intent.putExtras(bundle);

startActivity(intent);

Activity2:

ImageView image = (ImageView) findViewById(R.id.img2);
TextView txtTen= (TextView) findViewById(R.id.tv1);
Intent goiIntent=getIntent();
Bundle extras = goiIntent.getExtras();
byte[] b = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
image.setImageBitmap(bmp);
fasteque
  • 4,309
  • 8
  • 38
  • 50
  • Possible duplicate of [How do I pass data between activities on Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android) – Tim Dec 16 '15 at 19:31

2 Answers2

1

You should show us the logical to help us understand the problem.

However i think your problem is that the byte [] is to big to be pass in an intent.

One workaround could be to have an abstract class with a public static field of type byte[]. Before broadcasting your intent, set this field with your data and in the next activity read this data and don't forget to set the field to null when you don't need it to avoid memory leak.

Create a class ImageHelper as follow :

public abstract class ImageHelper {
    public static byte [] image;
}

In your activity 1, before launching the intent, instead of intent.putExtra("picture", b); use ImageHelper.image = b;

Then in your activity 2, instead of byte[] b = extras.getByteArray("picture");, you use byte[] b = ImageHelper.image;.

sonic
  • 1,894
  • 1
  • 18
  • 22
-1
Activity 1
intent.putExtra("BitmapImage", bitmap);

Activity 2
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
ImageView image = (ImageView) findViewById(R.id.img2);
image.setImageBitmap(bitmap);
  • 2
    I wouldn't suggest to pass a Bitmap in an Intent because it's very costly. Instead, pass around URIs or any other location mechanism (file path, etc.). There's an un-documented limit of 1MB which has been tested by other developers and anyway using extras is discouraged by Google: https://code.google.com/p/android/issues/detail?id=5878 – fasteque Dec 16 '15 at 20:20