Question

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"?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top