I've read so many threads in StackOverflow, but couldn't find the solution for my problem. I've been trying to fix the problem for the past 1 week, but couldn't figure it out. Would be great if someone could help me out,
Here is the problem,
I have nearly 50 images and I make use of ViewPager and Fragments to make the user swipe through the images. All the images are 250*350 dimensions which is not more than 20KB per image. On Swyping 30th-35th image I get OutOfMemory exception.. Here is the stack trace,
09-07 16:30:09.126: E/MessageQueue-JNI(1023): java.lang.OutOfMemoryError
09-07 16:30:09.126: E/MessageQueue-JNI(1023): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-07 16:30:09.126: E/MessageQueue-JNI(1023): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
09-07 16:30:09.126: E/MessageQueue-JNI(1023): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
09-07 16:30:09.126: E/MessageQueue-JNI(1023): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:541)
09-07 16:30:09.126: E/MessageQueue-JNI(1023): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:571)
Here is the code MyGallery.java,
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setOffscreenPageLimit(2);
pager.setAdapter(pageAdapter);
}
private List<Fragment> getFragments() {
List<Fragment> fList = new ArrayList<Fragment>();
String picture_book = "";
for (int i = 1; i <= 50; i++) {
picture_book = "picture" + i;
fList.add(MyGallery_Fragment.newInstance(picture_book));
}
return fList;
}
private class MyPageAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
And here is the MyGaller_Fragment class,
public static final MyGallery_Fragment newInstance(String picture) {
MyGallery_Fragment f = new MyGallery_Fragment();
contextForDialog = f.getActivity();
Bundle bdl = new Bundle();
bdl.putString("picture", picture);
f.setArguments(bdl);
return f;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
context = activity.getApplicationContext();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
picture = getArguments().getString("picture");
View v = inflater
.inflate(R.layout.mygallery_fragment, container, false);
picture_img = (ImageView) v.findViewById(R.id.picture_iv);
picture_resID = getResources().getIdentifier(picture, "drawable",
context.getPackageName());
picture_biticon = BitmapFactory.decodeResource(getResources(),
picture_resID);
picture_img.setImageBitmap(picture_biticon);
return v;
}
}
This is what I tried,
I was making use of
FragmentPagerAdapter
and changed toFragmentStatePagerAdapter
as mentioned here.. I thought thatFragmentStatePagerAdapter
just keeps the fragments' views directly left and right of the currently shown item and destroys the other items automatically.. but that didn't solve the problem,I even set
setOffScreenPageLimit
to 2, so that the old fragments will be destroyed.. but that didn't solve either.I tried destroying Bitmap after assigning the value, but got the same error.
I also tried this code in MyGallery_Fragment class,
@Override public void onDestroy() { super.onDestroy(); Drawable drawable = picture_img.getDrawable(); if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; picture_biticon = bitmapDrawable.getBitmap(); picture_biticon.recycle(); } }
I tested the app in Note II and it shows
outofmemory
between any of the 30th-35th image swipe, but the same code is working fine in my newly bought Moto E.
Would be great if someone could help me out to fix this problem.