1

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 dynamic image loading, so that if the user flips 2 images, I'll load next 2 to memory and delete 2 first ones. It kind of works, but it is ugly and I have trouble when user flips images back(imageflipper.showprevious()).

I can't really shift all images and place new images to the beginning.

My question is:
Is there a better way to do this kind of stuff? Resizing images didn't really help.

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
taivo
  • 139
  • 1
  • 10

2 Answers2

0

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();
            }
        }
Aerrow
  • 12,086
  • 10
  • 56
  • 90
0

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);
Korniltsev Anatoly
  • 3,676
  • 2
  • 26
  • 37