hassan moradnezhad
hassan moradnezhad

Reputation: 577

app close suddenly when i try to pass a bitmap to another activity in android

i try to pass bitmap from first activity to second activity
it works correctly , but when i try to change bitmap size , app closes suddenly

            Bundle bundle = new Bundle();
            double heightD = bitmap.getHeight() / 4.5;
            double widthD = bitmap.getWidth() / 4.5;
            int height = (int) heightD;
            int width = (int) widthD;
            bitmap = scaleDownBitmapImage(bitmap, width, height);
            bundle.putParcelable("key", bitmap);
            Intent intent = new Intent(getActivity(), CheckTokenImage.class);
            intent.putExtras(bundle);
            startActivity(intent);

it works , but if i change 4.5 to 4.4 or lower app crash and it stop suddenly!!
any help , please ?

Update : the "Unfortunately, MyApp has stopped" doesn't show , just it jump out, and i can open it from recently app list

Upvotes: 0

Views: 622

Answers (2)

Alireza Ghasemi
Alireza Ghasemi

Reputation: 589

Place large bitmaps in bundle by encoding to string :

//quality [0... 100]
public static String encodeImage(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) {
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    image.compress(compressFormat, quality, byteArrayOS);
    return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}

public static Bitmap decodeImage(String input) {
    byte[] decodedBytes = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}

It may consume some performances.

You can also write bitmap to file in cache folder and place file's path String in bundle.

Upvotes: 0

Vladyslav Matviienko
Vladyslav Matviienko

Reputation: 10881

Ok, now I see the problem. You try to pass Bitmap through Bundle.
You should not do that since Bundle size is limited, and Bitmap is likely too large. You should avoid passing large data through Bundle.

Instead you can save your Bitmap to the file, and pass file path through Bundle, and open in from file path in receiver.
Or use static field somewhere.

Upvotes: 2

Related Questions