1

I have been going crazy with this android error and from browsing the previous posts about this error none of the solutions that were given helped me.

I am of course talking about the all powerful killer known as the 'android Out of memory on a (NUMBER)-byte allocation.' I am trying to create a photo uploader and after the 3rd maybe 4th image my app crashes. I know to stop the crash to use a catch exception however that is not my problem. I am here to ask the community is there any solution to fixing this byte allocation error ?

Here is a snippet of my code involving the bit map .

 String post = editTextPost.getText().toString().trim(); // get the photo : Bitmap image = ((BitmapDrawable)postImage.getDrawable()).getBitmap(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // compress the image to jpg format : image.compress(Bitmap.CompressFormat.JPEG, 45, byteArrayOutputStream); byte[] imageBytes = byteArrayOutputStream.toByteArray(); String encodeImage = Base64.encodeToString(imageBytes,Base64.DEFAULT); // Recycle bitmap : image.recycle(); image=null; // send data : sendPost p = new sendPost(this); p.execute(post, encodeImage); imageBytes=null; 

Im not using any libraries and would like to keep it that way however if using a library is the only option I will use one. Hopefully someone can help.

1
  • I don't believe that recycle removes the image right away from memory. It depend on the garbage collector. recycle doesn't force it. Commented Mar 21, 2016 at 21:31

3 Answers 3

2

Bitmaps won't completely recycle if they are attached to a View, for example to a ImageView, if you do imageView.setImageBitmap(bitmap), you need to clear it before doing imageView.setImageBitmap(null) and clearing any other reference to any view.

Sign up to request clarification or add additional context in comments.

Comments

0

After you finish uplloading the image release the memory occupied by the "postImage" -

postImage.setImageDrawable(null); 

This will release any memory occupied by the bitmap associated with postImage

Comments

0

General Answer:

When you upload files you need to open a stream to your server and to your file at same time. read and write chunk by chunk of 1 MB for example. this way you will not get an out of memory exception.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.