chang gyu
chang gyu

Reputation: 21

how to transfer a bitmap to another activity?

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?

Upvotes: 1

Views: 319

Answers (2)

himangi
himangi

Reputation: 804

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);

Upvotes: 1

buzzingsilently
buzzingsilently

Reputation: 1586

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");

Upvotes: 2

Related Questions