I have an application in android that has an imageflipper. Problem is, after about 8 images loaded to memory, I get an out of memory error.
Well, I tried to do dyna
Use BitmapFactory.Options
BitmapFactory.Options opts = new BitmapFactory.Options();
opt.inSampleSize = 2;
//this will decrease bitmap size,
// but also affect quality of the image,
//so just play with this value to spot the good one;
Bitmap b = BitmapFactory.decodeFile(fileName, opts);
I used the below code snippet to over the memory related issues, We can over come this problem by the help of isRecycled()
method. Add this code with yours, here finalImage
is my BitmapDrawable
and R.id.image_viewer
is my imageview, you can change it yours
@Override
protected void onDestroy() {
finalImage.setCallback(null);
if (!((BitmapDrawable) finalImage).getBitmap().isRecycled()) {
((BitmapDrawable) finalImage).getBitmap().recycle();
}
finalImage = null;
unbindDrawables(findViewById(R.id.image_viewer));
Runtime.getRuntime().gc();
// scaledBitmap.recycle();
System.gc();
super.onDestroy();
}
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}