我什么时候应该压缩我的位图

时间:2017-10-02 21:26:46

标签: android bitmap compression

我正在从画布创建一个或多个位图图像,但我不确定我应该在哪里压缩位图图像,因为我经常耗尽内存。

    // create a bitmap from the canvas
    canvas.drawBitmap(printOutBitmap, width, 0, null);

    // should printOutBitmap be compressed here?

    // Initialize a new ByteArrayStream
    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    Bitmap[] imgs = new Bitmap[numImages];
    Bitmap bitmapImage;

    // splitting the bitmaps
    for ( int i = 0 ; i < imgs.length; i++ )
    {
        // create a single bitmap from the area of the original bitmap 
        bitmapImage = Bitmap.createBitmap(printOutBitmap, 0, y, printOutBitmap.getWidth(), h);

        // the returned bitmap needs to be a predefined width/height
        bitmapImage = Bitmap.createScaledBitmap( bitmapImage, width, height, false);

        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
        imgs[i] = bitmapImage;
    }

我压缩了printOutBitmap但仍然内存不足,而压缩每个位图需要花费太多时间。任何帮助或最佳做法&#39;我将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

compress()以压缩格式(PNG,JPEG,WebP)将Bitmap保存到磁盘。它更改Bitmap的内存使用情况。

摆脱ByteArrayOutputStreamcompress()调用,因为你正在做的就是分配一堆你没有使用的内存。