Android:ImageSwitcher中的位图导致OutOfMemory-Error

时间:2013-08-22 18:02:35

标签: android bitmap out-of-memory bitmapfactory imageswitcher

我需要你的帮助。我已经尝试了很多不同的东西来找出问题,但我仍然存在内存问题。 这是我的代码:

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);
      }

  }

}

Eclipse Memory Analyzer向我显示,内存以字节[]累积,大小为1,3 MB。位图的大小是问题吗?我不知道为什么内存会增长。

错误发生在" decodeStream()" BitmapFactory类的功能。

希望你能帮助我。

编辑: 每个位图的大小约为1,3 MB,也许它很大。我怎么能为我的imageswitcher实现位图缓存。我有6张图片,我每3秒钟加载一张图片。我想过一个"公共静态LRU-CACHE"?

1 个答案:

答案 0 :(得分:0)

根据您正在测试的Android版本,您可能会遇到与Bitmap相关的问题。实际的像素数据不会被垃圾收集,因此您必须在未使用的Bitmap对象上显式调用recycle方法(这已在以后的Android版本中修复)。

下一个问题是您的解码方法应该将位图缩小到所需的大小。你的图片可能太大了。