缓存列表视图的图片

发布于 2024-12-05 19:58:59 字数 1408 浏览 0 评论 0原文

所以我有一个包含来自某些网址的图像的列表视图,我尝试在加载位图数组列表后保存图片,但最终我的设备上的列表中仅显示 2-3 张图片(模拟器显示所有图片),所以我下载后尝试缓存图片,我使用: `

     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) {
                }
            }
        }

` 将图片保存在缓存中,然后我使用它从适配器中的缓存加载图片:

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

但我仍然从 9 中获取 3-4 张图片,这是内存问题吗? (所有图片加起来有300 kb)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2024-12-12 19:58:59

发现问题 Bitmap bmImg = BitmapFactory.decodeStream(is); 有错误并跳过慢速互联网连接上的数据,就像在真实设备上一样,所以我添加

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

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文