我正在从画布创建一个或多个位图图像,但我不确定我应该在哪里压缩位图图像,因为我经常耗尽内存。
// 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;我将不胜感激。谢谢!
答案 0 :(得分:0)
compress()
以压缩格式(PNG,JPEG,WebP)将Bitmap
保存到磁盘。它不更改Bitmap
的内存使用情况。
摆脱ByteArrayOutputStream
和compress()
调用,因为你正在做的就是分配一堆你没有使用的内存。