我正在尝试将数据发送到服务器。也就是说,一些字符串字段和一个图像,它被编码为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
我不明白为什么我会得到这个例外。我只是发送一张从三星Galaxay相机拍摄的常规图像(肯定不到16MB)。有什么想法吗?
编辑:我必须补充一点,第一块代码和第二块代码在不同的类上,所以第一块代码上的数据被传递给另一个类的对象,后者将它传递给另一个类的另一个对象class,具有HTTP请求。答案 0 :(得分:1)
为您的实体使用流式处理,以便它仅消耗流要求的小缓冲区大小。 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());
}
答案 1 :(得分:1)
最后,解决方案是按如下方式将图像压缩到50%:
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
它会缩小图像大小并使其小到足以将其作为字符串发送。
答案 2 :(得分:0)
完成后
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
你在内存中有两个版本的图像,一个是Bitmap,另一个是字节数组。通过Bitmap.recycle()
清理前者可能有所帮助。
未提供建议:用于传输图像的“多部分表单数据”不符合您的目的吗?这非常有效。