0

i am trying lazy loading with LruCache as shown here : Example using Androids lrucache.

public class TCImageLoader implements ComponentCallbacks2 {
    private TCLruCache cache;
    Context cv;
    public TCImageLoader(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(
            Context.ACTIVITY_SERVICE);
        int maxKb = am.getMemoryClass() * 1024;
        int limitKb = maxKb / 8; // 1/8th of total ram
        cache = new TCLruCache(limitKb);
        cv = context;
    }

    public void display(String url, ImageView imageview, int defaultresource) {
        imageview.setImageResource(defaultresource);
        Bitmap image = cache.get(url);
        if (image != null) {
            imageview.setImageBitmap(image);
        }
        else {
            new SetImageTask(imageview).execute(url);
        }
    }

    private class TCLruCache extends LruCache<String, Bitmap> {

        public TCLruCache(int maxSize) {
            super(maxSize);
        }

        @Override
        protected int sizeOf(String key, Bitmap value) {
            int kbOfBitmap = value.getByteCount() / 1024;
            return kbOfBitmap;
        }
    }

    private class SetImageTask extends AsyncTask<String, Void, Integer> {
        private ImageView imageview;
        private Bitmap bmp;
        public SetImageTask(ImageView imageview) {
            this.imageview = imageview;
        }

        @Override
        protected Integer doInBackground(String... params) {
            String url = params[0];
            try {
                //bmp = BitmapFactory.decodeResource(cv.getResources(), url);
                bmp = getBitmapFromURL(url);
                if (bmp != null) {
                    cache.put(url, bmp);
                }
                else {
                    return 0;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
            return 1;
        }

        @Override
        protected void onPostExecute(Integer result) {
            if (result == 1) {
                imageview.setImageBitmap(bmp);
            }
            //super.onPostExecute(result);
        }

        private Bitmap getBitmapFromURL(String src) {
            try {
                URL url = new URL(src);
                HttpURLConnection connection
                    = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                Bitmap myBitmap = BitmapFactory.decodeStream(input);
                return myBitmap;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    }

    @Override
    public void onLowMemory() {
    }

    @Override
    public void onTrimMemory(int level) {
    /*    if (level >= TRIM_MEMORY_MODERATE) {
            cache.evictAll();
        }
        else if (level >= TRIM_MEMORY_BACKGROUND) {
            cache.trimToSize(cache.size() / 2);
        }*/
    }
}

the TCimageloader is as shown in above code. my getview is as shown below :

public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//            imageView.setPadding(10,10,10,10);
        } else {
            imageView = (ImageView) convertView;
        }
       imagloader = new TCImageLoader(mContext);
        imagloader.display(mThumbIds[position], imageView, R.drawable.ic_launcher);
        //Picasso.with(mContext).load(mThumbIds[position]).placeholder(R.drawable.ic_launcher).into(imageView);
        imageView.setImageResource(R.drawable.ic_launcher);
        return imageView;
    }

but the problem i am facing is that when I scroll fast the getView has already been called and display method is already called. so the image views keep changing until the last called is implemented.

i am trying to make a custom gallery and this will also help me in downloading images from urls. I will have to use same method . will just have to change the file address to url address

Community
  • 1
  • 1
Parth Anjaria
  • 3,961
  • 3
  • 30
  • 62
  • [Check](http://square.github.io/picasso/) – Skynet Oct 23 '15 at 10:14
  • i have seen picasso. but the problem i am arising is that when i scroll down it get reloaded. – Parth Anjaria Oct 23 '15 at 10:24
  • 1
    Using picasso it solves your issue by using something like this: Picasso.with(CONTEXT).load(THE_IMAGE).into(ID_TO_IMAGE); – Luis Pereira Oct 23 '15 at 10:26
  • see in the code i have edited. i tried using like picasso like this already but getting issue like this. can you help me with how to use it if you have written any code – Parth Anjaria Oct 23 '15 at 10:28
  • I'm used to not read commented code sorry about that. I belive display method already sets the image to the passed imageview. Meaning you should not do imageView.setImageResource(R.drawable.ic_launcher); again – Luis Pereira Oct 23 '15 at 10:31
  • 1
    i am doing that cause its like placeholder of picasso. i mean until imageloader displays the image, the image\view will show iclauncher. and i either run picasso line or those 3 lines of imageloader and imageview. not both together. and the problem with picasso is that it loads images from the top. so if there are loads of images and if i scroll down directly then it will time to load the last images. so what i want is that when i scroll down the images at the bottom are loaded. that is possible wiith imageloader lines but the images are being loaded again and again – Parth Anjaria Oct 23 '15 at 10:34
  • You should put the resource on your imageview tag of the xml adapter, If you dont have xml and you are making everything dinamically you should at least call imageView.setImageResource(R.drawable.ic_launcher); before display method, because display is async and setImageResource not – Luis Pereira Oct 23 '15 at 10:36
  • thank you @iGoDa . the code was right. i had to just restart eclipse and it is running now. – Parth Anjaria Oct 23 '15 at 12:45
  • Glad it is working now, amazing eclipse bugs – Luis Pereira Oct 23 '15 at 12:47

0 Answers0