0

I´m getting out of memory errors on "slower" phones (512mb of RAM).

My app has some tabs on the main activity and in those tabs it has some image buttons that link to a webview. I think the error comes from loading ALL the images at once, even the ones that aren't seen on the first tab. I already tried the "android:largeHeap="true"" but it still crashes.

Can anyone help me make my app not load images until the tab where those images are is pressed? Changing the visibility to INVISIBLE or GONE doesn't help either.

EDIT:

I'm loading my images as buttons:

XML:

    <Button
    android:id="@+id/jn"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="3dp"
    android:background="@drawable/b_jn" />

JAVA:

    Button bjn = (Button) findViewById(R.id.jn);

    bjn.setOnClickListener(new View.OnClickListener() {

 @Override
     public void onClick(View v) {
 // TODO Auto-generated method stub
 startActivity(new Intent("applandia.quiosquedigitalfree.JN"));

        }
    });

Now I have about 50 of these in a row in the same activity. I'm not an expert coder.

Pini0n
  • 1
  • 2
  • How do you load your images? Please [edit] your question to include that code and the actual stack trace. – thegrinner Feb 25 '14 at 16:41
  • Please consider to add some code, which shows your problems clearly. Actually we have at least not enough information to help you out. – ztirom Feb 25 '14 at 16:59

2 Answers2

0

Can get a scaled bitmap or try other options like inSampleSize, do check how your solution affects the quality of your image too.

Esp for "..loading ALL the images at once, even the ones that aren't seen on the first tab.."(i have done the following for a similar implementation):
you can use inSampleSize to load all images and replace the visible tab images by the original ones- meaning for the selected tab, dynamically load images without the inSampleSize for the BitmapFactory and its Options.

You could scale them down depending on different devices to load the images without facing a memory problem. In your case, some phones may not exhibit the same behavior on the first run, but eventually, without handling an optimized image loading solution, app will crash.

Check more on this:

Topic under Load a Scaled Down Version into Memory.
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Out of memory error on Android

On how to avoid them:
How to avoid an out of memory error while using bitmaps in Android

For an overview:
http://blogs.innovationm.com/android-out-of-memory-error-causes-solution-and-best-practices/ http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html

Also, android:largeHeap
android:largeHeap="true" convention?

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51
-1

Well, there are so many ways to solve this proble, the fisrt one and better for me it's using a "Cache image loader", here it's a library created by Sergey Tarasevich, it's very useful.

https://github.com/nostra13/Android-Universal-Image-Loader

If you prefer to keep working like you're doing now, a simple but ugly solution it's to call:

System.gc() in order to indicate to system that you want to make a garbage collect (which will clean up some memory).

Ivan Verges
  • 595
  • 3
  • 10
  • 25