在显示整页图像时防止OOM

时间:2014-02-13 01:46:49

标签: android

我最近为我的应用程序实现了一个教程。本教程通过具有完整页面图像的viewpager完成。有10张图片大约170kb。我在测试时没有内存问题,但我看到有OOM异常的错误报告。我使用FragmentStatePagerAdapter并在片段被销毁时手动回收每个位图。

  private class TutorialPageAdapter extends FragmentStatePagerAdapter {

    private int[] tutorialImages;

    public TutorialPageAdapter(FragmentManager fm, int [] tutorialImages) {
        super(fm);
        this.tutorialImages = tutorialImages;
    }

    @Override
    public Fragment getItem(int i) {
        Bundle b = new Bundle();
        b.putInt(C.BUNDLE_TUTORIAL_IMAGE, tutorialImages[i]);
        b.putBoolean(TutorialPageFragment.FINAL_TUTORIAL_SCREEN, i == getCount()-1 && !singleTutorial);
        TutorialPageFragment frag = new TutorialPageFragment();
        frag.attachListener(TutorialFragment.this);
        frag.setArguments(b);
        return frag;
    }

    @Override
    public int getCount() {
        return tutorialImages.length;
    }
}

然后是视图寻呼机中的片段

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_tutorial_page, container, false);
    ButterKnife.inject(this, v);
    Bitmap bm = BitmapFactory.decodeResource(getResources(), getArguments().getInt(C.BUNDLE_TUTORIAL_IMAGE));
    tutorialImage.setImageBitmap(bm);
    if (getArguments().getBoolean(FINAL_TUTORIAL_SCREEN, false)) {
        finishTutorial.setVisibility(View.VISIBLE);
    }
    if (prefs.getBoolean(C.PREF_TUTORIAL_SHOW, false)) {
        _setupSwipeClose();
    }
    return v;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    Drawable drawable = tutorialImage.getDrawable();
    if (drawable != null && drawable instanceof BitmapDrawable) {
        ((BitmapDrawable) drawable).getBitmap().recycle();
    }
}

关于我如何防止OOM异常的任何提示?

0 个答案:

没有答案