0

When I try to scroll images with atleast 50 images, the application crashes.

"Failed to allocate a 841692 byte allocation with 233120 free bytes and 227KB until OOM"

FATAL EXCEPTION: main
Process: sstechnology.com.sssticks, PID: 24617
java.lang.OutOfMemoryError: Failed to allocate a 841692 byte allocation with 233120 free bytes and 227KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:613) 
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:446)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at android.content.res.Resources.createFromResourceStream(Resources.java:2813)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2514)
at android.content.res.Resources.loadDrawable(Resources.java:2416)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:393)
at android.content.res.Resources.getDrawable(Resources.java:800)
at android.content.Context.getDrawable(Context.java:403)
at android.widget.ImageView.resolveUri(ImageView.java:747)
at android.widget.ImageView.setImageResource(ImageView.java:398)
at sstechnology.com.jiprasticks.ImageAdapter.getView(ImageAdapter.java:59)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)

I am trying to set images to gridView which is in a fragment with tab layout. There are 10 tabs in which each of them has same layout file but with different images in gridView.

Here is the code for a single fragment.

 int[] pics={...}; // i am declaring R.drawable.name implicitly
    public ExpressionStickerFragment(Context context, int size)
{

    this.size = size;
    this.context=context;
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.sticker_fragment,container,false);
    GridView gridView = (GridView)view.findViewById(R.id.gridView);
    gridView.setAdapter(new ImageAdapter(context,pics,size));
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ShareClass shareClass=new ShareClass(context);
        //   System.out.println("=== "+position);
            shareClass.createPreview(pics[position],0);

        }
    });
    return view;
}

And here is the ImageAdapter

public class ImageAdapter extends BaseAdapter {

Context context;
int[] images;
int imageSize;
int h=0;
public ImageAdapter(Context context,int[] images ,int imageSize)
{
    this.imageSize=imageSize;
    this.context=context;
    this.images=images;

}

public ImageAdapter(Context context,int[] images ,int imageSize,int imageHeight)
{
    this.imageSize=imageSize;
    this.context=context;
    this.images=images;
     h=imageHeight;
}



@Override
public int getCount() {
    return images.length;
}

@Override
public Object getItem(int position) {
    return images[position];
}

@Override
public long getItemId(int position) {
    return images[position];
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView = new ImageView(context);
    try {


        imageView.setImageResource(images[position]);

        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        if(h==0)


        imageView.setLayoutParams(new GridView.LayoutParams(imageSize, imageSize));
        else
            imageView.setLayoutParams(new GridView.LayoutParams(imageSize, h));



    }
    catch (Exception e)
    {
        System.out.println("Error ==== " +e);
    }

    return imageView;
    }
}

Thanks in advance...

4
  • Your images may be too big. Commented May 26, 2016 at 17:45
  • no <=60kb atleast 50 images in each fragment and there are 10 tabs Commented May 26, 2016 at 17:48
  • I meant too big for the amount of memory you had left. Have you tried these solutions? Commented May 26, 2016 at 18:20
  • Thanks for the reply but I myself solved it using largeHeap in the manifest Commented May 26, 2016 at 19:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.