1

I am getting exception of out of memory in the below code.

try {
    String strBase64 = BitMapToString(bitmap);
    bitmap.recycle();
    JSONObject data = new JSONObject();
    data.put("image_data", strBase64.toString());
    data.put("uploadedBy", "1");
    Log.i("JSON DATA", data.toString());
    HttpClient httpClient = new DefaultHttpClient();
    String url = "http://**.**.***.***:91/bisqup/index.php?r=ws/AddAppImage";
    HttpPost httpPost = new HttpPost(url);
    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters.add(new BasicNameValuePair("data", data.toString()));
    httpPost.setEntity(new UrlEncodedFormEntity(parameters));
    HttpResponse response = httpClient.execute(httpPost);
    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
    String json = reader.readLine();
    JSONTokener tokener = new JSONTokener(json);
    JSONObject finalResult = new JSONObject(tokener);
    Log.i("Response", "" + finalResult.toString());
} catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
}

public String BitMapToString(Bitmap bitmap) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();
        String temp = Base64.encodeToString(b, Base64.DEFAULT);
        baos.close();
        baos = null;
        Log.i("BASE64", temp);
        return temp;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

unfortunately I am getting the exaception like this.

11-05 20:28:05.414: E/AndroidRuntime(23446): FATAL EXCEPTION: main
11-05 20:28:05.414: E/AndroidRuntime(23446): java.lang.OutOfMemoryError
11-05 20:28:05.414: E/AndroidRuntime(23446):    at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:140)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at java.lang.StringBuilder.append(StringBuilder.java:125)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at org.json.JSONStringer.string(JSONStringer.java:344)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at org.json.JSONStringer.value(JSONStringer.java:252)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at org.json.JSONObject.writeTo(JSONObject.java:667)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at org.json.JSONObject.toString(JSONObject.java:636)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at com.siliconithub.android.bisqup.BisqupImagePreview$2.onClick(BisqupImagePreview.java:134)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at android.view.View.performClick(View.java:2485)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at android.view.View$PerformClick.run(View.java:9080)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at android.os.Handler.handleCallback(Handler.java:587)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at android.os.Looper.loop(Looper.java:130)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at android.app.ActivityThread.main(ActivityThread.java:3687)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at java.lang.reflect.Method.invokeNative(Native Method)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at java.lang.reflect.Method.invoke(Method.java:507)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-05 20:28:05.414: E/AndroidRuntime(23446):    at dalvik.system.NativeStart.main(Native Method)

So can anyone tell me why this memory issue is rising with this code?

Any suggestion will be appreciated.

Rushabh Patel
  • 3,052
  • 4
  • 26
  • 58
  • 1
    line 134 is the log before or after the post? in any case, use a stream based json writer as well as a stream based base64 encoder – njzk2 Nov 05 '12 at 16:44

2 Answers2

1

Most probably your Bitmap is too big when you read it into a String, and that is why you get an OutOfMemory exception. You should make a Multipart-post and use a FileBody instead of a String for this kind of requests. You can have a look at How to make a multi-part POST in Java for more details.

Community
  • 1
  • 1
Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • 1
    I can use multipart POST, but at the side of web service,its not designed for multipart so it can not get my multipart POST. this is the limitation of web service.but yes multipart is the solution of this problem, that is fine, I completely agree with you. – Rushabh Patel Nov 06 '12 at 05:29
1

Try generating your JSON object without the JSONObject class, it seems fairly simple, so just build the string.

String data = "{ image_data: \"" + strBase64.toString() + "\", uploadedBy: \"1\" }";
Louis Ricci
  • 20,804
  • 5
  • 48
  • 62