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



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

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

bitmap.compress 可能会导致OOM错误。尝试使用 bitmapfactory.decodestream with bitmapfactory.options

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

最新更新