缓存列表视图的图片
所以我有一个包含来自某些网址的图像的列表视图,我尝试在加载位图数组列表后保存图片,但最终我的设备上的列表中仅显示 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现问题
Bitmap bmImg = BitmapFactory.decodeStream(is);
有错误并跳过慢速互联网连接上的数据,就像在真实设备上一样,所以我添加了
found the problem
Bitmap bmImg = BitmapFactory.decodeStream(is);
has bugs and skips data on slow internet conections like on a real device, so i addedand