1

When I capture using camera2 api,image is made and transfer the image to bytes next to bitmap. My purpose is to select save or not after capturing. So It will be not made in file before Pressing save btn.

below : send side

    Bitmap bitmap = textureView.getBitmap();
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,100,bs);
    byte[] byteArray = bs.toByteArray();

below : receieve side

    byte[] byteArray = getIntent().getByteArrayExtra("byteArray");
    bitmap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
    resultView.setImageBitmap(bitmap);

and I got received the error like below

android.os.TransactionTooLargeException

I understand the cause of error But I wanna transfer the image to another activity Is there anyone who help this?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
chang gyu
  • 21
  • 1
  • 1
    "But I wanna transfer the image to another activity" -- why? Why not perform both actions in the same activity, such as by using two fragments? – CommonsWare May 04 '18 at 13:40
  • 1
    Save the bitmap from first activity and access that bitmap from the second activity. It's a bad approach to transfer bitmap programmatically which can be done by using intent. https://stackoverflow.com/a/13226283/5492047 – Reyansh Mishra May 04 '18 at 13:42
  • 1
    Bitmap will be differ in size it can cause `TransactionTooLargeException`. So the best way is save the `Bitmap` in internal storage and pass the URL between Activities. Safe and sound . And Use a ImageLoader to load the Image from storage probably Glide. – ADM May 04 '18 at 14:49

2 Answers2

2

Put your bitmap object in Intent.putExtra("key", object),

intent.putExtra("btimap", bitmap);

Get it using Intent.getParcelableExtra("key"),

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("btimap");
buzzingsilently
  • 1,546
  • 3
  • 12
  • 18
  • 1
    that code has same error Caused by: android.os.TransactionTooLargeException: data parcel size 14746144 bytes – chang gyu May 04 '18 at 14:07
1

Convert it to a Byte array before you add it to the intent, send it out, and decode.

//Convert to byte array
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image",byteArray);


Then in Activity 2:

byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
himangi
  • 788
  • 4
  • 9