whats up folks.
I'm trying to make a product catalog and this is what i'm doing:
- Activity with ViewPager -- Each Product is a Fragment in this ViewPager. -- Each Fragment have a Gallery.
I download the products data and images from web and store the images into the sdcard.
But my app keep crashing and this is the error
05-22 10:04:43.829: E/AndroidRuntime(12388): FATAL EXCEPTION: main
05-22 10:04:43.829: E/AndroidRuntime(12388): java.lang.OutOfMemoryError
05-22 10:04:43.829: E/AndroidRuntime(12388): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
05-22 10:04:43.829: E/AndroidRuntime(12388): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
05-22 10:04:43.829: E/AndroidRuntime(12388): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
05-22 10:04:43.829: E/AndroidRuntime(12388): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:324)
I was using de bitmap factory inside the getView on Adapter of the Gallery, but it crashes when i swipe the Fragments. Then I changed and create and List when the Main Activity was created, but now if I close the activity and try to open it again, it crashes, THEN if i try to open again it opens normally.
What i`m doing wrong? There is a better way to do that?
this is my adapter to Gallery
public class ImagemAdapter extends BaseAdapter {
private Context ctx;
List<Bitmap> pics;
public ImagemAdapter(Context c, List<Bitmap> pics) {
ctx = c;
this.pics = pics;
}
@Override
public int getCount() {
return pics.size();
}
@Override
public Object getItem(int position) {
return pics.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
int arquivoLayout = R.layout.adp_galeria;
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(arquivoLayout, null);
ImageView img = ((ImageView)convertView.findViewById(R.id.imgProd));
img.setImageBitmap(pics.get(position));
}
return convertView;
}
}
another strange thing is, the convertView on the adapter is ALWAYS null.
EDIT:
I think the problem is that bitmap retains in the memory. When the onCreateView of the Fragment is called, i populate a List with the images i should show and when the onDestroyView of fragment is called, I do this:
for(int i =0; i< imagemBit.size(); i++){
Bitmap bmp = imagemBit.get(i);
bmp.recycle();
bmp = null;
}
imagemBit.clear();
BUT: When the onCreateView is called again (the user returned to a previous page) when I try to populate again the list the app crashes with OutOfMemory Exception.