I have a ListView
and a GridView
both causing me problems and OutOfMemory
errors when scrolling, I read online about the issue and saw this question so I used Sunil's first solution and implemented it in my code. the images for the GridView
and ListView
are at "/res/drawable/image1.png" and so on, I passed to the CustomAdapter
class this int array public static int[] mDrawableImg = {R.drawable.back, R.drawable.arrows, R.drawable.bomber, R.drawable.archers, R.drawable.knight};
and used this:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder = new Holder();
final View rowView = inflater.inflate(R.layout.program_list2, null);
holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
holder.img.setImageResource(imageId[position]);
// holder.img.setImageBitmap(convertBitmap(String.valueOf(imageId[position])));
return rowView;
}
Now as you see I added a line of code to load the image from Bitmap
with a function called convertBitmap
but my GridView
is empty, its scrollable meaning there are items but the images are not loaded.
convertBitmap
Function:
public static Bitmap convertBitmap(String path) {
Bitmap bitmap = null;
BitmapFactory.Options bfOptions = new BitmapFactory.Options();
bfOptions.inDither = false;
bfOptions.inTempStorage = new byte[32 * 1024];
File file = new File(path);
FileInputStream fs = null;
try {
fs = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if (fs != null) {
bitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fs != null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bitmap;
}
What have I done wrong and will it solve my OutOfMemory
errors? thanks.