解码位图时Android OutofmemoryError



大家好,我正在研究一个在服务器中上传图像文件的项目。我正在使用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);

尝试以下内容来压缩位图并编码。

 Bitmap selectedImage = getResizedBitmap(yourBitmaptoCompress, 350, 350);

定义GetResizedBitMap方法

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

编码图像位图(base64)

 String   encodedImage = encodeImage(selectedImage);

定义Encotimage方法

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

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

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

我希望它对您有帮助...

最新更新