当我尝试将位图传递给 android 中的另一个 activity 时,应用程序突然关闭

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

我尝试将位图从第一个activity传递到第二个activity
它工作正常,但是当我尝试更改位图大小时,应用程序突然关闭

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

它可以工作,但是如果我将 4.5 更改为 4.4 或更低 应用程序崩溃并且突然停止!!
有什么帮助吗?

更新:"Unfortunately, MyApp has stopped"没有显示,只是跳出来,我可以从最近的应用程序列表中打开它

好的,现在我看到问题了。您尝试通过 Bundle.
传递 Bitmap 您不应该这样做,因为 Bundle 大小有限,而 Bitmap 可能太大。您应该避免通过 Bundle 传递大数据。

相反,您可以将 Bitmap 保存到文件中,并通过 Bundle 传递文件路径,然后在接收器中从文件路径打开。
或者在某处使用静态字段。

通过编码为字符串将大位图放入包中:

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

可能会消耗一些性能。

您还可以将位图写入缓存文件夹中的文件,并将文件的 路径 字符串放入包中。