在Android中发布BASE64图像时出现java

在Android中发布BASE64图像时出现java

我在以下代码中遇到内存不足的异常。

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)


那么谁能告诉我为什么此代码会引起内存问题?

任何建议将不胜感激。

最佳答案

尝试生成不带JSONObject类的JSON对象,这似乎很简单,因此只需构建字符串即可。

String data = "{ image_data: \"" + strBase64.toString() + "\", uploadedBy: \"1\" }";

关于android - 在Android中发布BASE64图像时出现java.lang.OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13235141/

10-10 17:34