I need to create a ViewPager in Android with 5 slides, each of which consists of an image and text. I have an array with resources for images:
private static final int[] images = {R.drawable.tutorial_step_01, R.drawable.tutorial_step_02, R.drawable.tutorial_step_03, R.drawable.tutorial_step_04, R.drawable.tutorial_step_05, R.drawable.tutorial_step_06};
then I create an adapter:
@Override public Object instantiateItem(ViewGroup container, int position) { LinearLayout tv = (LinearLayout) inflater.inflate(R.layout.tut_slide, null); TextView title = (TextView) tv.findViewById(R.id.tut_title); title.setText(getResources().getText(titles[position])); TextView content = (TextView) tv.findViewById(R.id.tut_content); ImageView image = (ImageView) tv.findViewById(R.id.tut_image); slide_image = BitmapFactory.decodeResource(getResources(), images[position]); image.setImageBitmap(slide_image); content.setText(getResources().getText(contents[position])); ((ViewPager) container).addView(tv, 0); return tv; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView((LinearLayout) object);
//
}
the problem is that the android does not want to collect the image after selecting another page. Thus, after 10-15 changes, it leaves the OutOfMemory exception. Then I added to the string initialization
if (slide_image!= null) { slide_image.recycle(); System.gc(); }
And it works well! But except for one: I have a black screen instead of the first image, which is replaced with a real one after several coups. So I donβt know what to do with memory leak
source share