我想在保存到文件之前先壓縮字節數組。 當我用平減指數來壓縮字節數組,我得到OutOfMemoryError
:Android Deflator內存不足錯誤
ERROR/dalvikvm-heap(16065): Out of memory on a 921616-byte allocation.
I check the code and it is the same as android developer。但我添加了DeflatorOutputStream
以減少內存使用量。
我的代碼:
public static byte[] compress(byte[] data) throws IOException {
Deflater deflater = new Deflater();
deflater.setInput(data);
deflater.finish();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
DeflaterOutputStream dos=new DeflaterOutputStream(outputStream);
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int count=deflater.deflate(buffer);
// returns the generated code... index
dos.write(buffer, 0, count);
}
deflater.end();
byte[] output = outputStream.toByteArray();
dos.finish();
dos.close();
outputStream.close();
return output;
}
我檢查這個錯誤發生在這條線:int count=deflater.deflate(buffer);
的DeflaterOutputStream將再次做通縮!所以你要壓縮兩次。 – isnot2bad
好的,但這並不能解決內存不足錯誤 –