我在下面的代码中得到内存不足的例外。
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;
}
}
不幸的是,我正在接受这样的恶化。
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)
所以有人能告诉我为什么这个内存问题会随着这段代码而上升吗?
任何建议都将受到赞赏。
答案 0 :(得分:1)
当您将Bitmap
读入String
时,OutOfMemory
很可能太大,这就是您获得Multipart-post
例外的原因。您应该制作FileBody
并使用String
代替{{1}}来处理此类请求。您可以查看How to make a multi-part POST in Java了解更多详情。
答案 1 :(得分:1)
尝试在没有JSONObject类的情况下生成JSON对象,看起来相当简单,所以只需构建字符串。
String data = "{ image_data: \"" + strBase64.toString() + "\", uploadedBy: \"1\" }";