private void jsonDataInsert(int startIndex,int endIndex) throws JSONException{
JSONObject obj=new JSONObject(jsonText);
JSONArray items=obj.getJSONArray("recent");
int numOfItem=items.length();
Drawable d_thumb=null;
BitmapFactory.Options options=new BitmapFactory.Options();
options.inPurgeable=true;
options.inSampleSize=4;
// items의 모든 데이터 순회
for(int i=startIndex;i<numOfItem;i++){
if(endIndex<numOfItem&&i==endIndex){ break; }
JSONObject jsonString=items.getJSONObject(i);
try{
d_thumb=LoadImageFromWeb(jsonString.getString("src"),options);
}catch(Exception e){}
// 각 항목의 author의 title 키의 값을 하나의 문자열로 만들어서 data에 추가
data.add(new Item(d_thumb,jsonString.getString("subject")));
handler.sendEmptyMessage(0);
//d_thumb.setCallback(null);
}
}
private Drawable LoadImageFromWeb(String url,BitmapFactory.Options options) throws MalformedURLException,IOException{
InputStream is=(InputStream)new URL(url).getContent();
Drawable d=Drawable.createFromResourceStream(getResources(),null,is,"src name",options);
return d;
}
BitmapFactory.Option으로 inPurgeable, inSampleSize을 셋팅 합니다.
options.inPurgeable=true; 시스템이 메모리에서 반환하기를 원할때 메모리에서 제거된다.
options.inSampleSize=4; 1/4로 이미지를 가져오기때문에 outOfMemory문제가 해결된다.
이외에도 여러가지 시나리오에 따라 이미지리소스에 대한 뒤처리라던지 깔끔하게 해주어야 한다.
일반적으로 알려진 API Level 11부터(허니컴)는 Manifest에 application태그에 largeHeap="true" 옵션추가로 해결할 수 있는데 큰이미지를 받았을 경우 처리는 가능하지만 앱동작이 느려지는 단점이 있다.
이미지의 퀄리티가 많이 떨어지지않는 내에서 줄여서 가져와야 앱동작이 더 수월할것이다.
위의 상황도 아직 시나리오에 따른 코드를 더 정리하면 좋겠지만, Bad Case의 경우 저 정도로도 나은 성능이 나온다는 것을 확인할 수 있다.
'Programing > android' 카테고리의 다른 글
AsyncTask (0) | 2014.06.29 |
---|---|
안드로이드 Volley - Network & ImageLoading 라이브러리 (0) | 2014.06.28 |
Background Thread에서 UI 접근하기 (0) | 2014.06.27 |
Bluetooth 개발 01. 기초 (0) | 2014.06.25 |
안드로이드 타이틀바 및 인디케이터바 제거 (0) | 2014.05.15 |