简体   繁体   English

Android-压缩图像的位图数组

[英]Android -Compress bitmap array of images

I have a Bitmap [] images . 我有一个Bitmap [] images But line 但是线

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

causes error 导致错误

Cannot invoke compress(Bitmap.CompressFormat, int, ByteArrayOutputStream) on the array type Bitmap[] 无法在数组类型Bitmap []上调用compress(Bitmap.CompressFormat,int,ByteArrayOutputStream)

In my search I got codes to compresss to one bitmap.but nothing to compress Bitmap array. 在我的搜索中,我得到了压缩为一个位图的代码,但是没有压缩位图数组的代码。

code

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

You should compress the Bitmap array in a loop, like this: 您应该像这样循环压缩Bitmap数组:

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

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM