1

I need your help. I have tried a lot of different things to find out the problem, but i have still a memory problem. Here`s my Code:

public class AboutActivity extends Activity implements ViewSwitcher.ViewFactory {
//private WebView webview;
static final String BRANDING_PREFS_NAME = "BrandingInfo";

int[] imageIds = {0,1,2,3,4,5 };
int currentImageIndex = 0;
//Runnables
private final Handler handler = new Handler();
private ShowNextImageRunnable showNextImage;
/** Called when the activity is first created. */
/**@Override */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);

    ImageSwitcher imageSwitcher =    (ImageSwitcher)findViewById(R.id.imageSwitcher);
    imageSwitcher.setFactory(this);
    imageSwitcher.setImageResource(R.drawable.about);
    if(brandingInfo.getString("aboutImage0", "").length() > 0 ||
       brandingInfo.getString("aboutImage1", "").length() > 0 ||
       brandingInfo.getString("aboutImage2", "").length() > 0 ||
       brandingInfo.getString("aboutImage3", "").length() > 0 ||
       brandingInfo.getString("aboutImage4", "").length() > 0 ||
       brandingInfo.getString("aboutImage5", "").length() > 0) {
        handler.postDelayed(showNextImage, 0);
    }
}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onResume() {
    super.onResume();
}

public View makeView() {
      ImageView i = new ImageView(this);
      i.setBackgroundColor(0xFF000000);
      i.setScaleType(ImageView.ScaleType.FIT_START);
      i.setLayoutParams(new ImageSwitcher.LayoutParams(ImageSwitcher.LayoutParams.MATCH_PARENT,
              ImageSwitcher.LayoutParams.MATCH_PARENT));
      i.setAdjustViewBounds(true);
      return i; 
}

private boolean showNextImage(int index){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPurgeable = true;
    options.inInputShareable = true;
    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeStream(openFileInput("about"+index+".png"), null, options);
    } catch (FileNotFoundException e) {
        return false;
    }
    ImageSwitcher imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher);
    final ImageView g = (ImageView) imageSwitcher.getCurrentView();
    final Bitmap b = bitmap;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (b != null) {
                g.setImageBitmap(b);
            }
        }
    });
    bitmap = null;
    return true;
}

class ShowNextImageRunnable implements Runnable {

      public void run() {
        ++currentImageIndex;
        if(currentImageIndex >= imageIds.length)
            currentImageIndex = 0;

        if(!showNextImage(currentImageIndex))
            handler.postDelayed(showNextImage, 0);
        else
            handler.postDelayed(showNextImage, 3000);
      }

  }

}

The Eclipse Memory Analyzer shows me that, the memory is accumulated in byte[] with 1,3 MB. Is the size of the bitmap the problem? I don`t know why the memory grows.

The error occurs on the "decodeStream()" function of the BitmapFactory class.

Hope you can help me.

Edit: The size of each bitmap is ~ 1,3 MB, maybe it`s to big. How could I implement a bitmap caching for my imageswitcher. I have 6 images and i load a image each 3 seconds. I thought about a "public static LRU-CACHE"?

Wilhelm Dewald
  • 149
  • 1
  • 11
  • Its your total app that's using too much memory. Probably from loading too many bitmaps of too large a size. How big are the bitmaps? And you may want to implement caching so you don't keep old bitmaps around and/or explicitly recycle the old ones. – Gabe Sechan Aug 22 '13 at 18:07
  • Hi the size of the images is ~ 1,3MB. Is that to big? Could you give me a tip how to implement the caching? Thx – Wilhelm Dewald Aug 22 '13 at 18:23
  • 1 of them isn't, many of them can be. For caching, look at LRUCache, its in the support library. – Gabe Sechan Aug 22 '13 at 18:45

1 Answers1

0

Depending on the Android version you are testing on, you may run into a problem related to Bitmap. The actual pixel data doesn't get garbage collected so you have to explicitly call the recycle method on an unused Bitmap object (this has been fixed in later Android versions).

The next problem is that your decoding method should scale down the bitmap to the needed size. Your images may be way too big.

tiguchi
  • 5,392
  • 1
  • 33
  • 39
  • The Problem occurs only on Android 4.2.2. I`ve tried it with recycle, but didn`t help. Ist it maybe the size of the images? – Wilhelm Dewald Aug 22 '13 at 18:21
  • @WilhelmDewald Either way, you definitely should scale down bitmaps. The (Java) heap size for app processes is pretty limited and loading a 8MP photo for example just for displaying it in a 400x400 view is wasteful and slow. You should read the following article: http://developer.android.com/training/displaying-bitmaps/index.html – tiguchi Aug 22 '13 at 18:55
  • Ok. Maybe it is definitly one option. I will have a look on this option. Thank you Nobu – Wilhelm Dewald Aug 22 '13 at 19:31
  • Hi Nobu Games. Scaling the bitmap and implementing a LRUCache fixed the problem. Thx – Wilhelm Dewald Sep 21 '13 at 11:15