I’m trying to send data to a server. That is, some string fields and an image, which is encoded as base64 and passed as a String like this:
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;
And then, when preparing the HTTP PUT request:
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());
}
The error looks like this:
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
I don’t see why am I getting this exception. I’m just sending a regular image that was taken from a Samsung Galaxay camera (for sure less than 16MB). Any idea?
EDIT: I must add, that the first chunk of code and the second one are on different classes, so the data on the first chunk of code is passed to an object of another class, which passes it to another object of another class, which has the HTTP request.
At the end, te solution was to compress the image to 50% as follows:
It reduces the image size and makes it small enough to send it as a String.