error Caused by: java.lang.OutOfMemoryError Load Image

Posted by user2493770 on Stack Overflow See other posts from Stack Overflow or by user2493770
Published on 2014-05-26T21:19:49Z Indexed on 2014/05/26 21:25 UTC
Read the original article Hit count: 206

This is my method to load images in background, the first and second load normally. But after these loading, a memory error appears. How can I fix this?

public class MainArrayAdapterViewHolder extends ArrayAdapter<EmpresaListaPrincipal> {

private final Context context;
private ArrayList<EmpresaListaPrincipal> data_array;
public DisplayImageOptions options;
public ImageLoader imageLoader = ImageLoader.getInstance();

public MainArrayAdapterViewHolder(Context context, ArrayList<EmpresaListaPrincipal> list_of_ids) {

    super(context, R.layout.main_list_rowlayout, list_of_ids);
    this.context = context;
    this.data_array = list_of_ids;


    //------------- read more here https://github.com/nostra13/Android-Universal-Image-Loader 

    options = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.ic_launcher).showImageOnFail(R.drawable.ic_launcher).resetViewBeforeLoading()
            .cacheOnDisc().imageScaleType(ImageScaleType.IN_SAMPLE_INT).bitmapConfig(Bitmap.Config.RGB_565).delayBeforeLoading(0).build();

    File cacheDir = StorageUtils.getCacheDirectory(context);
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).memoryCacheExtraOptions(720, 1280) // default = device screen
                                                                                                                        // dimensions
            .discCacheExtraOptions(720, 1280, CompressFormat.JPEG, 100).threadPoolSize(3) // default
            .threadPriority(Thread.NORM_PRIORITY - 1) // default
            .memoryCacheSize(2 * 1024 * 1024).discCache(new UnlimitedDiscCache(cacheDir)) // default
            .discCacheSize(50 * 1024 * 1024).discCacheFileCount(100).discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
            .imageDownloader(new BaseImageDownloader(context)) // default
            .tasksProcessingOrder(QueueProcessingType.FIFO) // default
            .defaultDisplayImageOptions(options) // default
            .build();

    imageLoader.init(config);

}

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

     ViewHolder viewholder;

     View v = convertView;


    //Asociamos el layout de la lista que hemos creado e incrustamos el ViewHolder
        if(convertView == null){

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //View rowView = inflater.inflate(R.layout.main_list_rowlayout, parent, false);

             v = inflater.inflate(R.layout.main_list_rowlayout, parent, false);

             viewholder = new ViewHolder();


             viewholder.textView_main_row_title = (TextView) v.findViewById(R.id.textView_main_row_title);
                viewholder.imageView_restaurant_icon = (ImageView) v.findViewById(R.id.imageView_restaurant_icon);
                viewholder.textView_main_row_direccion = (TextView) v.findViewById(R.id.textView_main_row_direccion);
                v.setTag(viewholder);


        }

    ImageLoadingListener mImageLoadingListenr = new ImageLoadingListener() {

        @Override
        public void onLoadingStarted(String arg0, View arg1) {
            // Log.e("* started *", String.valueOf("complete"));

        }



        @Override
        public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
            // Log.e("* complete *", String.valueOf("complete"));
        }

        @Override
        public void onLoadingCancelled(String arg0, View arg1) {

        }

        @Override
        public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
            // TODO Auto-generated method stub

        }
    };

    try {

         viewholder = (ViewHolder) v.getTag();

          viewholder.textView_main_row_title.setText(data_array.get(position).getNOMBRE());
          viewholder.textView_main_row_direccion.setText(data_array.get(position).getDIRECCION());


        String image = data_array.get(position).getURL();


        // ------- image ---------
        try {
            if (image.length() > 4) imageLoader.displayImage(image, viewholder.imageView_restaurant_icon, options, mImageLoadingListenr);               
        } catch (Exception ex) {
        }

        //textView_main_row_title.setText(name);
        //textView_main_row_address.setText(address);

    } catch (Exception e) {
        // TODO: handle exception
    }

    return v;
}

public class ViewHolder {
    public TextView textView_main_row_title;
    public TextView textView_main_row_direccion;

    //public TextView cargo;
    public ImageView imageView_restaurant_icon;
}

}

© Stack Overflow or respective owner

Related posts about android

Related posts about out-of-memory