1

我正在尝试通过调用 web 服务上传设备相机拍摄的图像,服务器代码在 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();

这段代码昨天工作正常,但今天我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)
4

2 回答 2

3

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

    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);
于 2012-12-27T06:48:05.967 回答
1

我认为对于手机来说,字符串中的图像数据太多了。您可以改用FileEntity

那应该可以解决您的记忆问题。

于 2012-12-26T07:21:26.007 回答