0

Here is the source of PageF fragment used in ViewPager & PageAdapter.

public class PageF extends Fragment {
    public static final String ARG_PARAM1 = "bitmap";

    public static PageF newInstance(int id) {
        PageF fragment = new PageF();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup  container,
         Bundle savedInstanceState) {
        return inflater.inflate(R.layout.preview_image, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable final Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ImageView imageView = (ImageView) view;
        Bitmap bitmap = BitmapFactory.decodeResource(
                getResources()
                , getArguments().getInt(ARG_PARAM1)
        );
        imageView.setImageBitmap(bitmap);
    }

}

How can I recycle that bitmap? sometimes, in some devices, it happens Out of memory. :(

myoungjin
  • 895
  • 1
  • 13
  • 27

1 Answers1

1

You should probably avoid to create a Bitmap by yourself.

Look at this JavaDoc :

ImageView#setImageResource(int resId);

It will not prevent all Out of Memory(OOM) errors

Neige
  • 2,770
  • 2
  • 15
  • 21