이미지를 인터넷에서 단일 활동으로 보여주는 ViewFlipper를 만듭니다. fling 할 때 이미지를 imageView로 설정 한 다음 viewflipper에 추가하십시오. 그러나 OOM은 항상 약 20 개의 이미지를 보여준 후에 발생합니다. 나는 그것을 소홀히하기 위해 깨끗한 일을했는데 제대로 작동하지 않았다! 다음은 코드입니다.메모리 부족으로 인한 Android 비트 맵의 일부 문제
public class ImageCache {
static private ImageCache cache;
private Hashtable<Integer, MySoftRef> hashRefs;
private ReferenceQueue<Bitmap> q;
private class MySoftRef extends SoftReference<Bitmap> {
private Integer _key = 0;
public MySoftRef(Bitmap bmp, ReferenceQueue<Bitmap> q, int key) {
super(bmp, q);
_key = key;
}
}
public ImageCache() {
hashRefs = new Hashtable<Integer, MySoftRef>();
q = new ReferenceQueue<Bitmap>();
}
public static ImageCache getInstance() {
if (cache == null) {
cache = new ImageCache();
}
return cache;
}
private void addCacheBitmap(Bitmap bmp, Integer key) {
cleanCache();
MySoftRef ref = new MySoftRef(bmp, q, key);
hashRefs.put(key, ref);
}
public Bitmap getBitmap(int resId) {
Bitmap bmp = null;
if (hashRefs.containsKey(resId)) {
MySoftRef ref = (MySoftRef) hashRefs.get(resId);
bmp = (Bitmap) ref.get();
}
if (bmp == null) {
URL imgUrl = null;
try {
imgUrl = new URL("http:/example/images/" + resId
+ ".jpg");
HttpURLConnection conn = (HttpURLConnection) imgUrl
.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
bmp = BitmapFactory.decodeStream(is);
is.close();
addCacheBitmap(bmp, resId);
} catch (Exception e) {
e.printStackTrace();
}
}
return bmp;
}
private void cleanCache() {
MySoftRef ref = null;
while ((ref = (MySoftRef) q.poll()) != null) {
hashRefs.remove(ref._key);
}
}
public void clearCache() {
cleanCache();
hashRefs.clear();
System.gc();
System.runFinalization();
}
여기에는로드 이미지 코드가 나와 있습니다.
public void LoadImage(int n){
iv = new ImageView(this);
imageCache = new ImageCache();
Bitmap bm = imageCache.getBitmap(n);
iv.setImageBitmap(bm);
iv.setScaleType(ImageView.ScaleType.CENTER);
viewFlipper.addView(iv, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
}
확인이 링크 ... http://developer.android.com/training/displaying-bitmaps/index.html –
감사를 당신의 몫. 그것은 나를 도와줍니다. –
그래, 나를 위해 일했다 : http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Harry