gpt4 book ai didi

android - java.lang.OutOfMemoryError on line httpPost.setEntity(new UrlEncodedFormEntity(params)) 在 android 中以字符串格式发送图像时

转载 作者:行者123 更新时间:2023-11-29 16:10:17 25 4
gpt4 key购买 nike

我正在尝试通过调用网络服务上传设备摄像头拍摄的图像,服务器代码使用 PHP。

首先我将位图转换为字符串

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
String imageInString = Base64.encodeToString(byteArray, Base64.DEFAULT);

然后构造名值对对象

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("image", imageInString));
params.add(new BasicNameValuePair("img_desc", img_desc));
params.add(new BasicNameValuePair("amount", amount));
params.add(new BasicNameValuePair("request_type", "INSERT"));

然后,

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream = httpEntity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
inputStream.close();

这段代码昨天运行正常,但今天我在 httpPost.setEntity(new UrlEncodedFormEntity(params)); 行收到错误 java.lang.OutOfMemoryError/p>

那么如何消除这个错误呢?

日志猫是

 FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:266)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
at java.lang.Thread.run(Thread.java:1020)
Caused by: java.lang.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:96)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:147)
at java.lang.StringBuilder.append(StringBuilder.java:217)
at org.apache.http.client.utils.URLEncodedUtils.format(URLEncodedUtils.java:165)
at org.apache.http.client.entity.UrlEncodedFormEntity.<init>(UrlEncodedFormEntity.java:71)
at com.network.GetJSONFomURL.getJSONSrtringFromUrl(GetJSONFomURL.java:24)
at com.network.WebServices.uploadImage(WebServices.java:51)
at com.markphoto_activities.UploadActivity$MyAsyncTaskUploadImage.doInBackground(UploadActivity.java:130)
at com.markphoto_activities.UploadActivity$MyAsyncTaskUploadImage.doInBackground(UploadActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:252)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 4 more


Activity com.markphoto_activities.UploadActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@407aafc8 that was originally added here
android.view.WindowLeaked: Activity com.markphoto_activities.UploadActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@407aafc8 that was originally added here
at android.view.ViewRoot.<init>(ViewRoot.java:285)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:152)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)

最佳答案

试试这个代码而不是名称值对

    MultipartEntity reqEntity = new MultipartEntity();
try {
reqEntity.addPart("email", new StringBody(email));
reqEntity.addPart("image", new FileBody(new File(imagePath)));
reqEntity.addPart("img_desc", new StringBody(img_desc));
reqEntity.addPart("amount", new StringBody(amount));
reqEntity.addPart("request_type", new StringBody("INSERT"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

其中 imagePath 是图像文件的路径。然后代替 httpPost.setEntity(new UrlEncodedFormEntity(params)); 使用

httpPost.setEntity(reqEntity);

关于android - java.lang.OutOfMemoryError on line httpPost.setEntity(new UrlEncodedFormEntity(params)) 在 android 中以字符串格式发送图像时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14037213/

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