而加入大量的图像内存不足错误图像、错误、内存不足

2023-09-04 03:07:33 作者:故笙诉离歌

我将两个图像使用下面的code,但它抛出一个内存不足错误我的图片是1MB左右各。

I am joining two images using the code below but it throws an OutOfMemory error my images are around 1MB each.

private Bitmap overlayMark(String first, String second)
{
    Bitmap bmp1, bmp2;
    bmp1 = BitmapFactory.decodeFile(first);
    bmp2 = BitmapFactory.decodeFile(second);
    if (bmp1 == null || bmp2 == null)
        return bmp1;

    int height = bmp1.getHeight();
    if (height < bmp2.getHeight())
        height = bmp2.getHeight();

    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth() + bmp2.getWidth(), height,
            Bitmap.Config.ARGB_8888);// Out of memory
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, 0, 0, null);
    canvas.drawBitmap(bmp2, bmp1.getWidth(), 0, null);
    bmp1.recycle();
    bmp2.recycle();
    return bmOverlay;
}

更新:我试了下面两个答案,但它仍然没有allwoing我创造这么大尺寸的位图的问题是,所产生的位图过大的尺寸围绕2400x3200所以其走出去的记忆

Update: I tried below two answers but it still not allwoing me to create bitmap of such big size the problem is that the resultant bitmap is too large in size around 2400x3200 so its going out of memory.

我如何加入大的图像,不会出现内存不足?

推荐答案

在不加载图像到内存中,你可以得到图像的大小,使用inJustDe codeBounds。位图返回null,但所有的参数设置。您可以相应地缩小图像。

Without loading the image into memory, you CAN get the size of the image, using inJustDecodeBounds. The Bitmap returns null, but all the parameters are set. You can scale down the image accordingly.

如果您的JPEG图像各1 MIB,转换为BMP会占用大量的内存确实如此。您可以通过图像的尺寸很容易地计算出它的BMP等同。这样一个大的图像转换有望崩溃确实如此。 Android的限制了它的应用程序,以16 MIB VM只。

If your JPEG images are 1 MiB each, conversion to a BMP will take a lot of memory indeed. You can easily calculate its BMP equivalent by the dimensions of the image. Conversion of such a large image is expected to crash indeed. Android limits its apps to 16 MiB VM only.

也可以使用,而不是ARGB_8888 RGB_565。

Also use RGB_565 instead of ARGB_8888.

所以,你唯一的解决办法是: (一)要使用BitmapFactory.Options.inSampleSize缩减图象 要么 (二)采用Android NDK,其中16 MIB限制是不存在的。

So your only solution is: (a) To use BitmapFactory.Options.inSampleSize to scale down the image or (b) Use Android NDK where the 16 MiB limit isn't there.

 
精彩推荐
图片推荐