Android - 压缩图像的位图数组

Android -Compress bitmap array of images

我有一个Bitmap [] images。但是行

images.compress(Bitmap.CompressFormat.PNG, 100, bos);

导致错误

Cannot invoke compress(Bitmap.CompressFormat, int, ByteArrayOutputStream) on the array type Bitmap[]

在我的搜索中,我得到了压缩到一个 bitmap.but 没有压缩位图数组的代码。

代码

Bitmap[] images = {BitmapFactory.decodeResource(getResources(),R.drawable.candle1),BitmapFactory.decodeResource(getResources   
(),R.drawable.candl3),BitmapFactory.decodeResource(getResources(),R.drawable.senson),BitmapFactory.decodeResource(getResources(),R.drawable.lawn)}; 
ByteArrayOutputStream bos=new ByteArrayOutputStream();
 //  images.compress(Bitmap.CompressFormat.PNG, 100, bos);
img=bos.toByteArray();

你应该在一个循环中压缩位图数组,像这样:

byte[][] img = new byte[images.length][];

for (int i = 0; i < images.length; i++) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    images[i].compress(Bitmap.CompressFormat.PNG, 100, bos);

    // use a 2D array for img if you want to retain the byte arrays for all the bitmaps
    img[i] = bos.toByteArray();
}

希望对您有所帮助。