1

Hello guys I am working on a project that uploads image file in the server. I am sending a data to the server using Volley. The Data that I will be sending contains base64 string. I encountered OutOfMemoryError when I decoded an image file. Am I implementing it correctly? Here is my code:

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);
3
  • 2
    image .compress(Bitmap.CompressFormat.JPEG, 100, outStream); compress image before you send to server ...! Commented Sep 7, 2017 at 7:06
  • I already did in: encodedImage = UploadImageHelper.encodeImageBitmap(UploadImageHelper.scaleBitmap(image, 1000, 1000)); Commented Sep 7, 2017 at 7:13
  • you are encodedImage image not compress ..! once you compress the image its reduce the size of image... Commented Sep 7, 2017 at 7:18

2 Answers 2

1

Try the following to compress bitmap and encode it.

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

Define getResizedBitmap method

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

Encode image bitmap (base64)

 String   encodedImage = encodeImage(selectedImage);

Define encodeImage Method

 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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Bitmap selectedImage = getResizedBitmap(selectedImage, 350, 350); selectedImage is used twice?
the values of H and W which is 350, is it necessary to have 350? or how did u derived 350?
for more details, please refer stackoverflow.com/questions/477572/…
0

compress image before you send to server ...!

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

i hope its helpful to you ...!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.