1

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

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 内存分析器向我显示,内存以 byte[] 的形式累积,大小为 1,3 MB。位图的大小是问题吗?我不知道为什么内存会增长。

错误发生在 BitmapFactory 类的“decodeStream()”函数上。

希望您能够帮助我。

编辑:每个位图的大小约为 1,3 MB,也许它太大了。如何为我的图像切换器实现位图缓存。我有 6 张图片,每 3 秒加载一张图片。我想过一个“公共静态LRU-CACHE”?

4

1 回答 1

0

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

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

于 2013-08-22T18:11:54.767 回答