我正在尝试通过调用网络服务上传设备相机拍摄的图像,服务器代码在P​​HP中。

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

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();


这段代码昨天正常工作,但是今天我在行java.lang.OutOfMemoryError上遇到错误httpPost.setEntity(new UrlEncodedFormEntity(params));

那么如何清除此错误?

日志猫是

 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);

10-08 01:18