解码位图时的Android OutOfMemoryError

时间:2017-09-07 06:58:07

标签: android performance out-of-memory android-volley

大家好我正在研究一个在服务器上传图像文件的项目。我正在使用Volley向服务器发送数据。我将发送的数据包含base64字符串。我解码图像文件时遇到 OutOfMemoryError 。我正确实施吗?这是我的代码:

for (int i = 0; i < pathList.size(); i++) {
    //error occurred here 
         Bitmap image = BitmapFactory.decodeFile(pathList.get(i), options);
            Log.d("bitmapImage", "nisudSiya" + " " + image);
            encodedImage = UploadImageHelper.encodeImageBitmap(UploadImageHelper.scaleBitmap(image, 1000, 1000));
            stringUri.add(encodedImage);
        }
        StringRequest stringRequest = new StringRequest(Request.Method.POST, POST_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(CreatePostActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
                VolleyApp.getInstance().cancelPendingRequests(PUSH_MULTIPLE_IMAGES);
                mProgressUpload.dismiss();
                finish();
                Log.d("tyler", response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                Toast.makeText(CreatePostActivity.this, "Error Uploading!", Toast.LENGTH_SHORT).show();
                mProgressUpload.dismiss();
                Log.d("tagsdsad23", error.toString());
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                for (int i = 0; i < stringUri.size(); i++) {
                    Attachment attachment = new Attachment();
                    attachment.setType("image");
                    attachment.setUrl(stringUri.get(i));
                    shit.add(attachment);
                }
                Log.d("shitsize", String.valueOf(shit.size()));
                JSONArray jsonArray = new JSONArray();
                for (int i = 0; i < shit.size(); i++) {
                    try {
                        JSONObject jsonObject = new JSONObject();
                        Log.d("shittype", shit.get(i).getUrl());
                        jsonObject.put("type", shit.get(i).getType());
                        jsonObject.put("url", shit.get(i).getUrl());
                        jsonArray.put(jsonObject);
                        Log.d("tyler-gwapa", jsonObject.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                Log.d("tyler-gwapa", jsonArray.toString());

                Map<String, String> params = new HashMap<String, String>();
                params.put(USER_TOKEN, userToken);
                params.put(CAPTION, postContent.getText().toString());
                params.put(ATTACHMENT, jsonArray.toString());
                return params;
            }
        };
        VolleyApp.getInstance().addToRequestQueue(stringRequest, PUSH_MULTIPLE_IMAGES_TAG);

2 个答案:

答案 0 :(得分:1)

尝试以下方法压缩位图并对其进行编码。

 public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, int 
  bitmapHeight) {
    return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight, 
   true);
}

定义getResizedBitmap方法

 String   encodedImage = encodeImage(selectedImage);

对图像位图进行编码(base64)

 private String encodeImage(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
    byte[] b = baos.toByteArray();
    String encImage = Base64.encodeToString(b, Base64.DEFAULT);
    return encImage;
}

定义encodeImage方法

IF [Event]='Bought' then [Count of Occurences] ELSE 0 END

答案 1 :(得分:0)

在发送到服务器之前压缩图像......!

image .compress(Bitmap.CompressFormat.JPEG, 100, outStream);

我希望它对你有帮助......!