1

so i have a listview with images from some urls, i tryed to save the pictures after loading in a arrayList of bitmaps but in the end only 2-3 pictuers showed in the list on my device (the emulator shows all pictures), so i tried to cache the pictures after downloading and i use: `

     for (int i = 0; i < url.length; i++){
            URL urlAdress = new URL(url[i]);
            HttpURLConnection conn = (HttpURLConnection) urlAdress
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            Bitmap bmImg = BitmapFactory.decodeStream(is);

            // picList.add(bmImg);

            File cacheDir = context.getCacheDir();
            File f = new File(cacheDir, "000" + (i + 1));
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(f);
                bmImg.compress(Bitmap.CompressFormat.JPEG, 80, out);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null)
                        out.close();
                } catch (Exception ex) {
                }
            }
        }

` to save the pics in cache, and then i use this to load the pictures from cache in the adapter :

File cacheDir = context.getCacheDir();
    File f = new File(cacheDir, "000" + position);
    Drawable d = Drawable.createFromPath(f.getAbsolutePath());
    holder.icon.setImageDrawable(d);

but i still get 3-4 pictures from 9, is this a memory issue ? (all the pics together have 300 kb)

user924941
  • 943
  • 3
  • 12
  • 24
  • refer this http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview – ilango j Sep 23 '11 at 05:38

1 Answers1

0

found the problem Bitmap bmImg = BitmapFactory.decodeStream(is); has bugs and skips data on slow internet conections like on a real device, so i added

Bitmap bmImg = BitmapFactory.decodeStream(new FlushedInputStream(is));

and

static class FlushedInputStream extends FilterInputStream {
    public FlushedInputStream(InputStream inputStream) {
        super(inputStream);
    }

    @Override
    public long skip(long n) throws IOException {
        long totalBytesSkipped = 0L;
        while (totalBytesSkipped < n) {
            long bytesSkipped = in.skip(n - totalBytesSkipped);
            if (bytesSkipped == 0L) {
                int b = read();
                if (b < 0) {
                    break; // reached EOF
                } else {
                    bytesSkipped = 1; // read one byte
                }
            }
            totalBytesSkipped += bytesSkipped;
        }
        return totalBytesSkipped;
    }
}
user924941
  • 943
  • 3
  • 12
  • 24