user2645941 February 2016

OutofMemory Error when loading Images in Gridview

I'm implementing the following code in my adapter when i faced the problem of OutofMemory Error.But problem here is i am able to load only the first enter code here image that is 0th position in the gridview. Below is my code, Please suggest the changes.

public class GridAdapter extends BaseAdapter {

    String[] names;
    int[] images;
    Context context;
    private static LayoutInflater inflater=null;

    //private LruCache<String bitmap="",> memorucache;
    private LruCache<String, Bitmap> mMemoryCache;


    public  GridAdapter(Context mainActivity,String[] _names,int[] _images){

        names=_names;
        images=_images;
        context=mainActivity;
        inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final int maxMemory= (int) (Runtime.getRuntime().maxMemory() / 1024);
        final int cacheSize=maxMemory/8;

        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                // The cache size will be measured in kilobytes rather than
                // number of items.
                return bitmap.getByteCount() / 1024;
            }
        };

    }

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

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

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

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

        Holder holder=new Holder();
        View rowView;

        rowView=inflater.inflate(R.layout.gridlist,null);
        holder.txtview= (TextView) rowView.findViewById(R.id.textView1);
        holder.imageview= (ImageView) rowView.findViewById(R.id.imageView1);

        holder.txtview.setText(names[position]);
        loadBitmap(images[        

Answers


Shoeb Siddique February 2016

Please add this in Your Manifest application tag.

 android:largeHeap="true"

EDIT

ImageLoader

imageLoader.loadImage(imageUri, new SimpleImageLoadingListener()  
{ 
    @Override 
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) 
    { 
         // Do whatever you want with Bitmap 
    } 
}); 


Mustansar Saeed February 2016

Your images are too large to be loaded in to the memory. Do the following steps:

Hope this helps.


Soham Dutta February 2016

You could try using Universal Image Loader(https://github.com/nostra13/Android-Universal-Image-Loader ) or Picasso(http://square.github.io/picasso/) which I feel has a simpler way of implementation.

But if you dont wish to use libraries then you should add the line

android:largeHeap="true"

to your manifest.


Devendra Singh February 2016

try this

public Bitmap resizedBitmap(Bitmap originalImage, int width, int height) {
    Bitmap background = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);
    float originalWidth = originalImage.getWidth(), originalHeight = originalImage.getHeight();
    Canvas canvas = new Canvas(background);
    float scale = width / originalWidth;
    float xTranslation = 0.0f, yTranslation = (height - originalHeight * scale) / 2.0f;
    Matrix transformation = new Matrix();
    transformation.postTranslate(xTranslation, yTranslation);
    transformation.preScale(scale, scale);
    Paint paint = new Paint();
    paint.setFilterBitmap(true);
    canvas.drawBitmap(originalImage, transformation, paint);
    return background;
}

Added a color filter.

Post Status

Asked in February 2016
Viewed 3,684 times
Voted 11
Answered 4 times

Search




Leave an answer