안드로이드 개발 질문/답변
(글 수 45,052)
버튼에 이미지를 입히기 위해 ResourceLoader 클래스를 만들어 하나로 뭉쳐져 있는 비트맵 정보를 불러오게 만들었습니다.
BitmapFactory를 사용하면 생성되는 bitmap을 recycle()해줘야 한다고 들었는데, 제 짧은 식견으로는 어떻게 해야 할지 당췌 감이 안잡힙니다.
코드를 보고 조언을 해주세요.
public Bitmap GetBitmapImage(int i)
{
Bitmap retBitmap = null;
byte[] BinaryData = null;
byte[] DecompressedData = null;
byte[] CompressedData = null;
int DecompressedSize;
BinaryData = GetCompressedImage(i);
ByteArrayInputStream bais = new ByteArrayInputStream(BinaryData);
DecompressImage ImageInfo = new DecompressImage(bais);
CompressedData = new byte[(int)ImageInfo.comSize];
try {
bais.read(CompressedData);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DecompressedSize = m_pkHeader.kPosInfo[i].iWidth * m_pkHeader.kPosInfo[i].iHeight * 4 + 54 + 2; //32비트 이미지라서 4를 곱합 헤더 54바이트 2는 자꾸 모자라길래...
DecompressedData = Unzip(CompressedData, DecompressedSize);
//여기서 OOM 익셉션이 발생합니다.
retBitmap = BitmapFactory.decodeByteArray(DecompressedData, 0, DecompressedSize);
// if(retBitmap != null)
// {
// retBitmap = Bitmap.createBitmap(retBitmap, 0, 0, (int)ImageInfo.bmWidth, (int)ImageInfo.bmHeight);
// }
return retBitmap;
}
public Drawable GetDrawableImage(int i)
{
return new BitmapDrawable(GetBitmapImage(i));
}
이런식으로 만들어져 있고,
불러쓰는 형식은
Button myButton = (Button) find....;
myButton.SetBackgroundDrawable(loader.GetDrawableImage(5));
이렇게 해주고 있습니다. 이렇게 된 코드를 어디에서 비트맵 해제를 해줘야 할 지 모르겠습니다.
도움을 주세요 ㅠㅠ




private void RecycleBackground(ViewGroup group)
{
View[] childViews = getChildViews(group);
for (View view : childViews)
{
if(view instanceof ViewGroup)
{
RecycleBackground((ViewGroup)view); //재귀돌림
if((view.getBackground()) != null)
{
((BitmapDrawable)view.getBackground()).getBitmap().recycle();
System.gc();
}
}
else if(view instanceof Button)
{
if((view.getBackground()) != null)
{
((BitmapDrawable)view.getBackground()).getBitmap().recycle();
System.gc();
}
}
}
}
private View[] getChildViews(ViewGroup group){
int childCount = group.getChildCount();
final View[] childViews = new View[childCount];
for (int index =0;index < childCount; index++){
childViews[index] = group.getChildAt(index);
}
return childViews;
}
이런식으로 액티비티의 모든 뷰를 돌면서 해제를 해보려고도 했는데요,
이건 잘못된 캐스팅...이 나오네요 ㅠ