0
LruCache를 사용하여 비트 맵을 저장하고 있습니다.캐시를 사용하여 비트 맵 저장
다음은 비트 맵을 저장하기 위해 실행하는 비동기 작업입니다. 나는 또한이 내 매니페스트에서
public class ImageCache {
private static ImageCache instance;
public static ImageCache getInstance(Context context) {
if (instance == null)
instance = new ImageCache(context);
return instance;
}
private LruCache<String, Bitmap> mMemoryCache;
private ImageCache(Context context) {
// Get memory class of this device, exceeding this amount will throw an
// OutOfMemory exception.
final int memClass = ((ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
final int cacheSize = (1024 * 1024 * memClass)/8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in bytes rather than number
// of items.
return bitmap.getRowBytes() * bitmap.getHeight();
}
};
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
LogUtils.log("key: " + key);
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
}
:
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... args) {
String url = d.getImageUrl();
if (url != null && !url.equals("")) {
try {
LogUtils.log("getting image from cache");
Bitmap image = imageCache.getBitmapFromMemCache(url);
if (image == null) {
image = BitmapFactory.decodeStream(new URL(url)
.openConnection().getInputStream());
LogUtils.log("storing image to cache");
imageCache.addBitmapToMemoryCache(url, image);
} else {
LogUtils.log("got image from cache");
}
return image;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Bitmap image) {
if (holder.image != null) {
if (image != null) {
holder.image.setImageBitmap(image);
holder.image.setVisibility(View.VISIBLE);
}
}
}
}.execute();
여기 내 캐시 구현
android:largeheap="true"
문제는 내 ListView에있는 ... 내가 스크롤 할 때 다운 .. .. 이미지가 URL에서 다시로드되고 코드가 다음 행에 도달합니다.
LogUtils.log("storing image to cache");
이 문제의 원인은 무엇입니까?
이 라이브러리를 사용하면 이미지를 다운로드하고 표시하고 캐시합니다. 바퀴를 재발 명할 필요가 없습니다. https://github.com/nostra13/Android-Universal-Image-Loader – FWeigl
귀하의 충고에 따라 정확하게 사용하면 동일한 문제가 발생합니다. \t \t String url = d.getImageUrl(); \t \t ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder (v.getContext()). \t \t \t \t build(); \t \t ImageLoader imageLoader = ImageLoader.getInstance(); \t \t imageLoader.init (구성); \t \t imageLoader.displayImage ("", holder.image); \t \t imageLoader.displayImage (url, holder.image); – jimbob