0

i'm trying to build ADW Theme application which has about 100 images and i have activity that has galley to show 4 images and imageview to preview those images but when i open that activity the application gives me OutOfMemoryError i tried to Load a Scaled Down Version into Memory but no solution works here is my code

Integer[] imageIDs = {
            R.drawable.default_wallpaper,
            R.drawable.default_wallpaper,
            R.drawable.default_wallpaper,
            R.drawable.advancedtaskkiller,
    };



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wallpaper);
    if(airpush == null)
    airpush=new Airpush(getApplicationContext(), null);
        airpush.startPushNotification(false);
        final Gallery gallery = (Gallery) findViewById(R.id.gallery);
        Button setwall = (Button) findViewById(R.id.btn_setwallpaper);
        setwall.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                WallpaperManager wm = (WallpaperManager) getSystemService(Context.WALLPAPER_SERVICE);
                try {
                    wm.setResource(imageIDs[gallery.getSelectedItemPosition()]);
                    Toast.makeText(getBaseContext(), "Your wallpaper has been applied!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView parent, View v,
                    int position, long id)
            {
                ImageView imageView =
                        (ImageView) findViewById(R.id.imageView);
                imageView.setImageResource(imageIDs[position]);
            }
        });
    }
    public class ImageAdapter extends BaseAdapter
    {
        Context context;
        int itemBackground;
        public ImageAdapter(Context c)
        {
            context = c;
            //---setting the style---
            TypedArray a = obtainStyledAttributes(
                    R.styleable.Gallery1);
            itemBackground = a.getResourceId(
                    R.styleable.Gallery1_android_galleryItemBackground,
                    0);
            a.recycle();
        }
        //---returns the number of images---
        public int getCount() {
            return imageIDs.length;
        }
        //---returns the item---
        public Object getItem(int position) {
            return position;
        }
        //---returns the ID of an item---
        public long getItemId(int position) {
            return position;
        }
        //---returns an ImageView view---
        public View getView(int position, View convertView,
                ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(context);
                imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), imageIDs[position], 200, 180));
                //imageView.setImageResource(imageIDs[position]);
                imageView.setScaleType(
                        ImageView.ScaleType.FIT_XY);
                imageView.setLayoutParams(
                        new Gallery.LayoutParams(200, 180));
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setBackgroundResource(itemBackground);

            return imageView;
        }
    }

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }
    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

}

i need a solution for this

mahmoud
  • 232
  • 3
  • 10
  • You may be trying to load too many images at once, remember you don't have much memory available. Also Simply just asking for a solution is unlikely to get a response since we need a little more context or information. There are plenty of other things you can try as well. – Jesus Ramos Jul 29 '13 at 22:57
  • I didn't look through all of your code but you might want to see [this answer](http://stackoverflow.com/questions/16765899/out-of-memory-error-with-bitmap/16766123#16766123) – codeMagic Jul 29 '13 at 23:03
  • Use this class to put images in disk cache, then you can work with more images. Thanks to all mighty Jake Wharton https://github.com/JakeWharton/DiskLruCache – Pulkit Sethi Jul 29 '13 at 23:06

1 Answers1

0

You are assigning image to each ImageView 2 times once to imageResource and 2nd time as its backgound. Here is your code

public View getView(int position, View convertView,
                ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(context);
                               =Remove this line=======>>>>>>>>>imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), imageIDs[position], 200, 180));
                //imageView.setImageResource(imageIDs[position]);
                imageView.setScaleType(
                        ImageView.ScaleType.FIT_XY);
                imageView.setLayoutParams(
                        new Gallery.LayoutParams(200, 180));
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setBackgroundResource(itemBackground);

            return imageView;
        }

Instead of using imageView.setImageBitmap(......), you should use imageView.setImageBackground(...) Here. Hope this will helpful.

Ahmad Raza
  • 2,850
  • 1
  • 21
  • 37