누구든지 비트 맵을 처리하는 방법에 도움이 될 수 있습니다. VM buget 오류를 초과합니다. 사실 나는 listview에 더 많은 이미지를 표시했다. 그것은 잘 작동하고 활동을 반복 시작 후 비트 맵 오류를 throw 모든 답변은 진심으로 감사드립니다.비트 맵 크기를 처리하는 방법이 android에서 오류를 초과합니다
0
A
답변
1
공개 열거 형 BitmapLoading { INSTANCE;
private final Map<String, SoftReference<Bitmap>> cache;
private final ExecutorService pool;
private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
private Bitmap placeholder;
BitmapLoading() {
cache = new HashMap<String, SoftReference<Bitmap>>();
pool = Executors.newFixedThreadPool(5);
}
public void setPlaceholder(Bitmap bmp) {
try {
placeholder = bmp;
} catch (OutOfMemoryError e) {
placeholder.recycle();
bmp.recycle();
placeholder = null;
bmp = null;
}
}
public Bitmap getBitmapFromCache(String url) {
if (cache.containsKey(url)) {
return cache.get(url).get();
}
return null;
}
public void queueJob(final String url, final ImageView imageView,final int width, final int height) {
/* Create handler in UI thread. */
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String tag = imageViews.get(imageView);
if (tag != null && tag.equals(url)) {
if (msg.obj != null) {
imageView.setImageBitmap((Bitmap) msg.obj);
} else {
imageView.setImageBitmap(placeholder);
Log.d(null, "fail " + url);
}
}
}
};
pool.submit(new Runnable() {
@Override
public void run() {
Bitmap bmp = null;
try{
bmp = downloadBitmap(url, width, height);
Message message = Message.obtain();
message.obj = bmp;
handler.sendMessage(message);
}catch (OutOfMemoryError e) {
bmp.recycle();
bmp = null;
}
}
});
}
public void loadBitmap(final String url, final ImageView imageView,final int width, final int height) {
imageViews.put(imageView, url);
Bitmap bitmap = null ;
try {
bitmap = getBitmapFromCache(url);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
imageView.setImageBitmap(placeholder);
queueJob(url, imageView, width, height);
}
} catch (OutOfMemoryError e) {
bitmap.recycle();
bitmap = null;
System.gc();
}
}
private Bitmap downloadBitmap(String url, int width, int height) {
Bitmap bitmap =null;
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(url).getContent());
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
cache.put(url, new SoftReference<Bitmap>(bitmap));
return bitmap;
}catch (OutOfMemoryError e) {
bitmap.recycle();
bitmap = null;
System.gc();
}
catch (MalformedURLException e) {
} catch (IOException e) {
}
return null;
}
}
0
2
이러한 링크가 유용 할 수 있습니다.
http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html
또한이 링크는 이미지로드와 관련된 목록/그리드 구현시 간단하지만 과격한 실수를 어떻게 저 지르는지 보여줍니다.
How to solve the vm budget memory heap error in lazy loading bitmaps?
또한 활동을 반복적으로 호출 할 때 dis 문제가 나타나는 것처럼 어댑터의 getView 메소드에서 초기화중인 스레드에 이미지를로드하고있는 것으로 생각됩니다.
이것은 내가 꿰매고, 사람들이 구현 한 다음 유사한 문제에 부딪히는 공통점이 있습니다.
코드에서 이런 일이 발생하면 getView가 호출 된 모든 목록 행에 대한 스레드 생성으로 이어 지므로 다시 확인해야합니다.
0
사실이 프로젝트에 목록보기에 게으른로드 작업을 수행하는 방법의 아주 좋은 예가 : Android Lazy Loading ListView.
관련 문제
- 1. 프레임 애니메이션 있음 - 비트 맵 크기를 제거하는 방법은 Android에서 VM 예산 오류를 초과합니다.
- 2. "비트 맵 크기가 VM 예산을 초과합니다"오류를 극복하는 방법은 무엇입니까?
- 3. 안드로이드 비트 맵 크기가 VM 예산 오류를 초과합니다.
- 4. "비트 맵 크기가 VM 예산을 초과합니다"
- 5. ViewPager : 비트 맵 크기가 VM 예산을 초과합니다.
- 6. 비트 맵 크기가 VM 예산을 초과합니다. java.lang.OutOfMemoryError : 비트 맵 크기가 VM 예산을 초과합니다.
- 7. OutOfMemoryError : 비트 맵 크기가 VM 예산을 초과합니다
- 8. java.lang.OutOfMemoryError : 비트 맵 크기가 VM 예산을 초과합니다.
- 9. 비트 맵 크기가 VM 예산을 초과합니다.
- 10. "비트 맵 크기가 VM 예산을 초과합니다"
- 11. OutOfMemoryError에서 비트 맵 크기가 초과되었습니다. 비트 맵 크기가 VM 예산을 초과합니다.
- 12. java.lang.outofmemoryerror 비트 맵 크기가 비트 맵에서 비트 맵 크기를 초과했습니다
- 13. 비트 맵 캔버스의 크기를 조절하십시오
- 14. Android에서 바로 편집하는 비트 맵
- 15. Android 메모리 누출? 조각이있는 비트 맵이 VM 예산 오류를 초과합니다.
- 16. 게임 개발시 비트 맵 크기가 VM 예산을 초과합니다.
- 17. 안드로이드 ListView OutOfMemoryError : 비트 맵 크기가 VM 예산을 초과합니다
- 18. Android : 이미지로드 오류 : OutOfMemoryError : 비트 맵 크기가 VM 예산을 초과합니다.
- 19. "인덱스가 매트릭스 크기를 초과합니다"
- 20. Cocos2D android java.lang.OutOfMemoryError : 비트 맵 크기가 VM 예산을 초과합니다.
- 21. ImageView : OutOfMemoryError : 비트 맵 크기가 기기의 VM 예산을 초과합니다.
- 22. java OutOfMemoryError : 비트 맵 크기가 VM 예산을 초과합니다.
- 23. 비트 맵 크기의 Android 애니메이션 크래시가 VM 예산을 초과합니다
- 24. 프레임 단위의 애니메이션입니다. java.lang.OutOfMemoryError : 비트 맵 크기가 VM 예산을 초과합니다.
- 25. Android : 비트 맵 크기가 VM 예산을 초과합니다. 캔버스에 이미지로드
- 26. Android OutOfMemoryError : 비트 맵 크기가 VM 예산을 초과합니다.
- 27. 위젯 업데이트 용 RemoteView가 최대 비트 맵 메모리 사용 오류를 초과합니다
- 28. java.lang.OutOfMemoryError라는 메모리가 누출되는 것을 방지하는 방법 : 비트 맵 크기가 android에서 VM 예산을 초과합니다.
- 29. Android에서 이미지 다운로드 서버에서 OutOfMemoryError가 발생했습니다. 비트 맵 크기가 VM 예산 예외를 초과합니다.
- 30. ANDROID : outofmemory 비트 맵이 예산 크기를 초과합니다. 재활용하는 방법?
이 코드가 도움이 될 것 같아요. –