I am loading images from webserver using AQuery and getting out of memory exception (java.lang.OutOfMemoryError: thread creation failed) . I did everything that docs suggest while there are OOM. but i am not able to escape out. can you please suggest me why i am getting OOM after loading 40 images ?
-
What have you done to avoid OOM and that does not work?Alexander– Alexander2016-05-17 07:48:16 +00:00Commented May 17, 2016 at 7:48
-
Can you please share your code snippet??UnnY– UnnY2016-05-17 07:57:40 +00:00Commented May 17, 2016 at 7:57
-
BitmapAjaxCallback.clearCache(); AQUtility.cleanCacheAsync(mContext, triggerSize, targetSize); aq.id(R.id.image_center) .progress(R.id.progressbar2) .image(urlString, false,false, 200, 0); @ Alexander KulyakhtinJerin Raj– Jerin Raj2016-05-17 08:05:03 +00:00Commented May 17, 2016 at 8:05
-
if you using lower version and the memory is less then also you getting this errorAshwini Bhat– Ashwini Bhat2016-05-17 08:08:33 +00:00Commented May 17, 2016 at 8:08
-
I have tested my app on Samsung S6 EdgeJerin Raj– Jerin Raj2016-05-17 10:07:13 +00:00Commented May 17, 2016 at 10:07
3 Answers
The best way to avoid a OOM especially when dealing with images is as follows:
- If possible and your api supports, then try to fetch images based on resolution just enough for the viewing on the mobile device.
Using volley or Picasso on its own does not solve the OOM issue.. In order to handle the OOM issue, make use of the BitmapFactory provided by the framework to resize the downloaded image to a size just enough for the view in which it is being displayed. You can do something as follows
ImageView imgView = findViewById(R.id.img);
int height = imgView.getHeight();
int width = imgView.getWidth();
Bitmap = yourBitmapOfFullResolution;
Bitmap scaledBitmap = Bitmap.createScaledBitmap (yourBitmapOfFullResolution, height, width, true);
imgView.setImageBitmap(scaledBitmap);
1 Comment
Try Volley Library for this. its much faster and efficient. and i think your problem is the heap size of your device, if you are running and testing your app on emulator. then you can easily increase the heap size by editing the device configuration.
Thrown when a request for memory is made that can not be satisfied using the available platform resources. Such a request may be made by both the running application or by an internal function of the VM.
Add this feild in application tag of manifest file
android:largeHeap="true"