我尝试在android中压缩大字符串像这样:
try {
String str = "MyLarge String";
ByteArrayOutputStream rstBao = new ByteArrayOutputStream(str.length());
GZIPOutputStream zos = new GZIPOutputStream(rstBao);
zos.write(str.getBytes());
IOUtils.closeQuietly(zos);
byte[] bytes = rstBao.toByteArray();
String compressedString = Base64.encodeToString(bytes, true);
Log.i("Compressed", compressedString);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但是此代码返回了内存错误
outofmemory返回" Base64.encodeToString(bytes,true);"线
如果有人知道,请帮帮我
谢谢。
答案 0 :(得分:0)
试试这个
public static String compressString(String str) throws IOException{
if (str == null || str.length() == 0) {
return str;
}
BufferedWriter writer = null;
try{
File file = new File("your.gzip")
GZIPOutputStream zip = new GZIPOutputStream(new FileOutputStream(file));
writer = new BufferedWriter(new OutputStreamWriter(zip, "UTF-8"));
writer.append(str);
}
finally{
if(writer != null){
writer.close();
}
}
}