gpt4 book ai didi

android - 在 android 中发布 BASE64 图像时出现 java.lang.OutOfMemoryError

转载 作者:行者123 更新时间:2023-11-29 21:56:05 27 4
gpt4 key购买 nike

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com