2

After going through lot of memory allocation error solutions, I implied and tried many of those for my app, but I don't know why it is still giving crash for memory.It only happens for the camera folder images not for others.Please help me with correcting my code if it is wrong somewhere. First is On Activity result after selecting image using image intent:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Uri filePath = data.getData();

            try {
                //getting image from gallery
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);

                //Setting image to ImageView
                ivImage.setImageBitmap(bitmap);
                Uri selectedImageUri = data.getData();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

The other comes the method where I am uploading image to server by converting bitmap to byte array:

public void upload()
{
     progressDialog = new ProgressDialog(AddPropertyThird.this);
                progressDialog.setMessage("Uploading, please wait...");
                progressDialog.show();

                //converting image to base64 string
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = false;
                options.inPreferredConfig = Bitmap.Config.RGB_565;
                options.inDither = true;

              ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byte[] imageBytes = stream.toByteArray();
                bitmap.recycle();

                final String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);

                //sending image to server
                StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        progressDialog.dismiss();

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("volleyerror", "" + volleyError);
                    }
                }) {
                    //adding parameters to send
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> parameters = new HashMap<String, String>();
                        parameters.put("ImgStr", imageString);

                        return parameters;
                    }
                };

                RequestQueue rQueue = Volley.newRequestQueue(AddPropertyThird.this);
                rQueue.add(request);
            }

This is my logcat error:

 E/dalvikvm-heap: Out of memory on a 4192272-byte allocation.
Out of memory on a 4186240-byte allocation.
 Out of memory on a 2089032-byte allocation.
 FATAL EXCEPTION: main
                                                                    java.lang.OutOfMemoryError
                                                                      java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122)
user1111
  • 139
  • 8
  • http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object ...Hope this will help you. – Jaimin Thakkar Feb 10 '17 at 06:15
  • Possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – Carl Poole Feb 10 '17 at 06:17
  • I have already gone through the recommended link. And it didnt solve the problem @JaiminThakkar – user1111 Feb 10 '17 at 06:21
  • gone through it already didnt solve so posted a question. Thankyou @carlpoole – user1111 Feb 10 '17 at 06:22
  • You have not explained what your code should do. Should we find out our selfs? – greenapps Feb 10 '17 at 08:01
  • I can see a couple of solutions increase the heap size, compress and scale the image or write your own class to handle image upload where you can buffer your output. – Naveen Dissanayake Feb 10 '17 at 10:15

1 Answers1

0

For the first error check out this question for how to load images while avoiding Out of Memory errors.

You're getting an Out of Memory error with the ByteArray because you're pulling the image into memory from the stream on the line

byte[] imageBytes = stream.toByteArray();

So instead you would want to avoid doing that. Check out this answer.

Community
  • 1
  • 1
Carl Poole
  • 1,970
  • 2
  • 23
  • 28
  • can you please explain a bit more? actually in the link they have used multipart for image upload and thats not my case. – user1111 Feb 10 '17 at 06:32