将位图上传到服务器时Android的高内存使用率(100MB)

时间:2015-05-04 15:17:29

标签: android bitmap heap-memory

我正在尝试解决有时会导致OutOfMemoryException的问题。我只是将位图上传到服务器然后退出活动。我试图回收我的位图,但碎片仍然有点大。这是我的代码:

private class UploadTask extends AsyncTask<Bitmap, Void, Void> {

    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(NewEvent.this);
        pDialog.setMessage("Uploading data...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();

    }


    protected Void doInBackground(Bitmap... bitmaps) {
        if (bitmaps[0] == null)
            return null;
        setProgress(0);

        Bitmap bitmap = bitmaps[0];
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); // convert Bitmap to ByteArrayOutputStream
        InputStream in = new ByteArrayInputStream(stream.toByteArray()); // convert ByteArrayOutputStream to ByteArrayInputStream
        bitmap.recycle();
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            HttpPost httppost = new HttpPost(
                    "my server"); // server

            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("fileToUpload",
                    System.currentTimeMillis() + ".jpg", in);
            httppost.setEntity(reqEntity);

            Log.i("TAG", "request " + httppost.getRequestLine());
            HttpResponse response = null;
            try {
                response = httpclient.execute(httppost);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if (response != null)
                    try {
                        response1 = EntityUtils.toString(response.getEntity());
                        response1 = response1.replace("\n", "").replace("\r", "");
                        Log.i("TAG", "response " + response1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

            } finally {

            }
        } finally {

        }

        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Void result) {

        super.onPostExecute(result);
        pDialog.dismiss();
        uploadJSON();
    }
}

1 个答案:

答案 0 :(得分:0)

bitmap.compress 可能会导致OOM错误。尝试将 BitmapFactory.decodeStream BitmapFactory.Options

一起使用
BitmapFactory.Options options = new BitmapFactory.Options();
options.outHeight=400;
options.outWidth=600;
BitmapFactory.decodeStream(inputStream, null, options);