gpt4 book ai didi

android - 带有图像的 UrlEncodedFormEntity 时内存不足

转载 作者:行者123 更新时间:2023-11-29 00:42:27 26 4
gpt4 key购买 nike

我正在尝试将数据发送到服务器。也就是说,一些字符串字段和一个图像,它被编码为 base64 并作为一个字符串传递,如下所示:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
//Cleaning memory
try {
stream.close();
stream = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Eecode base64
nameValuePairs.add(new BasicNameValuePair(DatosDB.KEY_IMG, Base64.encodeToString(byteArray, Base64.DEFAULT)));
byteArray = null;

然后,在准备 HTTP PUT 请求时:

 try{
HttpClient httpclient = new DefaultHttpClient();
//PUT
HttpPut httpput = new HttpPut(KEY_121 + ruta);
//The exception is thrown when executing next instruction
httpput.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httpput);

HttpEntity entity = response.getEntity();
is = entity.getContent();

}catch(Exception e){
Log.e(TAG, "Error in http connection "+e.toString());
}

错误看起来像这样:

12-20 00:24:11.622: ERROR/AndroidRuntime(7499): FATAL EXCEPTION: main
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): java.lang.OutOfMemoryError
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:140)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at java.lang.StringBuilder.append(StringBuilder.java:125)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at java.net.URLEncoder.encode(URLEncoder.java:109)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at org.apache.http.client.utils.URLEncodedUtils.encode(URLEncodedUtils.java:184)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at org.apache.http.client.utils.URLEncodedUtils.format(URLEncodedUtils.java:163)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at org.apache.http.client.entity.UrlEncodedFormEntity.<init>(UrlEncodedFormEntity.java:71)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at com.android.upvar.Http_Request.put(Http_Request.java:171)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at com.android.upvar.Http_Request.poi(Http_Request.java:108)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at com.android.upvar.NewPOI.putPOI(NewPOI.java:360)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at com.android.upvar.NewPOI.access$6(NewPOI.java:325)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at com.android.upvar.NewPOI$5.onClick(NewPOI.java:157)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at android.view.View.performClick(View.java:2538)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at android.view.View$PerformClick.run(View.java:9152)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at android.os.Handler.handleCallback(Handler.java:587)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at android.os.Handler.dispatchMessage(Handler.java:92)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at android.os.Looper.loop(Looper.java:130)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at java.lang.reflect.Method.invokeNative(Native Method)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at java.lang.reflect.Method.invoke(Method.java:507)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-20 00:24:11.622: ERROR/AndroidRuntime(7499): at dalvik.system.NativeStart.main(Native Method)
12-20 00:24:11.632: WARN/ActivityManager(3923): Force finishing activity com.android.upvar/.NewPOI
12-20 00:24:11.636: ERROR/(3923): Dumpstate > /data/log/dumpstate_app_error

我不明白为什么会收到此异常。我只是发送一张从 Samsung Galaxay 相机拍摄的常规图像(肯定小于 16MB)。有什么想法吗?

编辑:我必须补充一点,第一段代码和第二段代码在不同的类中,因此第一段代码中的数据被传递给另一个类的对象,该对象又传递给另一个类的另一个对象类,它有 HTTP 请求。

最佳答案

为您的实体使用流式处理,以便它仅消耗流请求的小缓冲区大小。 ByteArrayOutputStream 将所有数据保存在内存中。

HTTPClient - Performance Guide

尝试像下面这样的实现,它不会在内存中创建不必要的对象。以下可能不是完整的实现。祝您编码愉快!

class BitMapRequestEntity extends AbstractHttpEntity {

private Bitmap bitmap;

public BitMapRequestEntity(Bitmap mBitmap) {
super();
this.bitmap = mBitmap;
}

@Override
public InputStream getContent() throws IOException,
IllegalStateException {
// TODO Auto-generated method stub
return null;
}

@Override
public long getContentLength() {
// TODO Auto-generated method stub
return 0;
}

@Override
public boolean isRepeatable() {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isStreaming() {
return true;
}

@Override
public void writeTo(OutputStream outstream) throws IOException {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
}

}
try {
org.apache.http.client.HttpClient httpclient = new DefaultHttpClient();
// PUT
HttpPut httpput = new HttpPut(KEY_121 + ruta);
// The exception is thrown when executing next instruction
httpput.setEntity(new BitMapRequestEntity(mBitmap));
HttpResponse response = httpclient.execute(httpput);

HttpEntity entity = response.getEntity();
is = entity.getContent();

} catch (Exception e) {
Log.e(TAG, "Error in http connection " + e.toString());
}

关于android - 带有图像的 UrlEncodedFormEntity 时内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8569060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com