B
B
Billy Milligan2014-08-24 17:50:07
Java
Billy Milligan, 2014-08-24 17:50:07

How to load image by URL in ListView and store it in cache?

How to properly download an image from the server and cache it on the device. I would like to achieve the same effect as in the VK application, so that without connecting to the network you can upload a couple of records.

public class RecipeListAdapter extends BaseAdapter {

    LayoutInflater inflater;
    ArrayList<Map<String, String>> list;

    public RecipeListAdapter(Context context, ArrayList<Map<String, String>> list) {
        this.list = list;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        View vi = inflater.inflate(R.layout.recipe_list_item, null);
        ImageView img = (ImageView) vi.findViewById(R.id.wallpaper);
        new LoadImage(img, list.get(position).get("ImageUrl"), list.get(position).get("id")).execute();
        return vi;
    }

    class LoadImage extends AsyncTask<Void, Void, Bitmap> {

        private ImageView iv;
        private String url;
        private String id;
        private boolean inCache = false;

        public LoadImage(ImageView iv, String url, String id) {
            this.iv = iv;
            this.url = url;
            this.id = id;
        }

        @Override
        protected Bitmap doInBackground(Void... Objects) {
            Bitmap bitmap = null;
            
            /*
            //Для примера, не знаю как реализовать
            bitmap = CacheImages.get(id);
            
            //Если есть в кэше то загрузить из него, иначе загрузить с сервера
            if(bitmap != null) {
                inCache = true;
                return bitmap;
            } else {
                //Загрузка картинку по урл
                // ... код для загрузки, не знаю как реализовать ...
                return bitmap;
            }
            */

            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            if(result != null && iv != null){
                //Если нет в кэше, то добавить
                
                if(!inCache) {
                    //CacheImage.add(id, result);
                }
                
                iv.setImageBitmap(result);
            }else{
                Log.i("Error", "image not uploaded, url: "+url);
            }
        }
    }
}

Правильно ли я пытаюсь это сделать? И как собственно правильно реализовать загрузку и кэширование?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Gamega, 2014-08-24
@Billy_Milligan

I implemented this using picaso, checked if there was a picture on the carriage, if not, loaded it from the network into imageview, after which I simply saved the picture from imageview to the card. Maybe a little hacky, but it works well.

C
Copperfield, 2014-08-24
@Copperfield

Use ready-made image loaders: UIL, picasso, volley etc.
All of them can asynchronously load and cache from the box.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question