Android:java.lang.OutOfMemoryError:在使用gson.toJson()时,在OOM之前分配可用字节和70MB失败

Android:java.lang.OutOfMemoryError:在使用gson.toJson()时,在OOM之前分配可用字节和70MB失败,android,json,gson,out-of-memory,Android,Json,Gson,Out Of Memory,在我的Android应用程序中,当我尝试将数据同步到大服务器时,我遇到了以下异常。我认为,当数据大小超过20MB时,会出现这种异常。我正在使用base64编码将位图图像保存为字符串,然后缩小图像大小,从而生成如此巨大的数据 04-18 13:51:51.957 16199-16816/com.example.myproject.app E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 128887990 byte allocation

在我的Android应用程序中,当我尝试将数据同步到大服务器时,我遇到了以下异常。我认为,当数据大小超过20MB时,会出现这种异常。我正在使用base64编码将位图图像保存为字符串,然后缩小图像大小,从而生成如此巨大的数据

04-18 13:51:51.957  16199-16816/com.example.myproject.app E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 128887990 byte allocation with 16777216 free bytes and 70MB until OOM"
04-18 13:51:52.037  16199-16816/com.example.myproject.app E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-4482
Process: com.example.myproject.app, PID: 16199
java.lang.OutOfMemoryError: Failed to allocate a 128887990 byte allocation with 16777216 free bytes and 70MB until OOM
    at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
    at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:146)
    at java.lang.StringBuffer.append(StringBuffer.java:219)
    at java.io.StringWriter.write(StringWriter.java:167)
    at com.google.gson.stream.JsonWriter.string(JsonWriter.java:570)
    at com.google.gson.stream.JsonWriter.value(JsonWriter.java:419)
    at com.google.gson.internal.bind.TypeAdapters$16.write(TypeAdapters.java:426)
    at com.google.gson.internal.bind.TypeAdapters$16.write(TypeAdapters.java:410)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:112)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:239)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
如何解决这个问题?我知道,当我使用Gson将数据从类转换为json时,就会出现这种情况。下面是我的代码:

SimpleDateFormat dtf=新的SimpleDateFormat(“yyyy-MM-dd HH:MM:ss”,Locale.ENGLISH);
GsonBuilder=新的GsonBuilder();
registerTypeAdapter(Date.class,新的JsonDeserializer()){
@凌驾
公共日期反序列化(JsonElement json,类型类型,JsonDeserializationContext反序列化Context)引发JsonParseException{
字符串frStr=json.getAsJsonPrimitive().getAsString();
日期retDate=null;
试一试{
retDate=dtf.parse(frStr);
}捕获(解析异常){
e、 printStackTrace();
}
归还日期;
}
});
registerTypeAdapter(Date.class,新的JsonSerializer()){
@凌驾
公共JsonElement序列化(日期src、类型typeOfSrc、JsonSerializationContext){
字符串jsDate=dtf.format(src);
返回新的JsonPrimitive(jsDate);
}
});
registerTypeAdapter(字节[].class,新的JsonDeserializer(){
@凌驾
公共字节[]反序列化(JsonElement json,类型类型,JsonDeserializationContext反序列化Context)引发JsonParseException{
返回Base64.decode(json.getAsString(),Base64.NO_WRAP);
}
});
gson=builder.create();
attDataAcc.设置attList(attList);
字符串jsonattacts=gson.toJson(attDataAcc,AttachmentDataList.class);
HttpEntity entityAtt=新的HttpEntity(jsonattacts,headers);
ResponseEntity restResA=restemplate.exchange(strUrl+“/saveAttToServer”,HttpMethod.POST,entityAtt,String.class);
公共类附件实现了可序列化{
@数据库字段(columnName=“id”,id=true)
私有字符串id;
@数据库字段(columnName=“用户id”)
私有整数用户标识;
@数据库字段(columnName=“附件\u id”)
私有字符串attachmentId;
@数据库字段(columnName=“文件名”)
私有字符串文件名;
@DatabaseField(columnName=“文件\数据”)
私有字符串文件数据;
@DatabaseField(columnName=“date”,dataType=dataType.date)
私人日期;
公共附件(){
超级();
//TODO自动生成的构造函数存根
}
公共附件(字符串id、整数用户id、字符串attachmentId、字符串文件名、字符串文件数据、日期){
this.id=id;
this.userId=userId;
this.attachmentId=attachmentId;
this.fileName=文件名;
this.fileData=fileData;
this.date=日期;
}
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共整数getUserId(){
返回用户标识;
}
public void setUserId(整数userId){
this.userId=userId;
}
公共字符串getAttachmentId(){
返回附件ID;
}
public void setAttachmentId(字符串attachmentId){
this.attachmentId=attachmentId;
}
公共字符串getFileName(){
返回文件名;
}
public void setFileName(字符串文件名){
this.fileName=文件名;
}
公共字符串getFileData(){
返回文件数据;
}
public void setFileData(字符串fileData){
this.fileData=fileData;
}
公共日期getDate(){
返回日期;
}
公共作废设置日期(日期){
this.date=日期;
}
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
附件,即=(附件)o;
如果(id!=null?!id.equals(that.id):that.id!=null)返回false;
如果(userId!=null?!userId.equals(that.userId):that.userId!=null)返回false;
如果(attachmentId!=null?!attachmentId.equals(that.attachmentId):that.attachmentId!=null)返回false;
如果(fileName!=null?!fileName.equals(that.fileName):that.fileName!=null)返回false;
if(fileData!=null?!fileData.equals(that.fileData):that.fileData!=null)返回false;
如果(date!=null?!date.equals(that.date):that.date!=null)返回false;
}
@凌驾
公共int hashCode(){
int result=id!=null?id.hashCode():0;
result=31*result+(userId!=null?userId.hashCode():0);
result=31*result+(attachmentId!=null?attachmentId.hashCode():0);
result=31*result+(fileName!=null?fileName.hashCode():0);
result=31*result+(fileData!=null?fileData.hashCode():0);
result=31*result+(date!=null?date.hashCode():0);
返回结果;
}
@凌驾
公共字符串toString(){
还我们