메뉴 건너뛰기

?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄


관련글은

이며, 관련글도 차례로 보는게 더 도움이 되실겁니다. 

기본적인 뼈대는 구글 블로그에 공개되어 있는 멀티 쓰레드를 이용한 이미지 다운로드 소스입니다. 
여기에 URL을 이용한 파일 캐싱 기능과 이미지 크기가 너무 커서 BitmapFactory에서 decoding 도중에
bitmap size exceeds VM budget 오류를 발생하며 종료되는 부분을 처리했습니다.


그리고, Bitmap 이미지를 파일로 저장하는 과정에서

bitmap.compress(CompressFormat.JPEG, 100, out);
bitmap.compress(CompressFormat.PNG, 100, out); 

2가지 옵션을 사용할 수 있는데

이미지를 JPEG로 압축할 경우, 특정 사진들에 대해서 화질이 너무 안 좋다는 단점이 있었고
이미지를 PNG로 압축할 경우에는 화질은 JPEG보다 우수하지만 압축하는데 시간과 리소스가
너무 오래걸린다는 단점이 있었습니다. (둘다 이미지를 압축하기 때문에 시간 및 리소스가 소비됩니다.)


그래서 그냥 네트워크로 받은 이미지를 원본 그대로 파일로 저장한다음, 그 파일에서 BitmapFactory.decodeFile을 
이용해서 불러오는 것으로 바꾸었습니다. 



다음은 파일에서 이미지를 bitmap size exceeds VM budget 오류를 발생시키지 않고 안전하게 불러오는
부분입니다. 

01.// File 에서 이미지를 불러올 때 안전하게 불러오기 위해서 만든 함수
02.// bitmap size exceeds VM budget 오류 방지용
03.Bitmap SafeDecodeBitmapFile(String strFilePath)
04.{
05.    File file = new File(strFilePath);
06.    if (file.exists() == false)
07.    {
08.        return null;
09.    }
10.     
11.    // 가로, 세로 최대 크기 (이보다 큰 이미지가 들어올 경우 크기를 줄인다.)
12.    final int IMAGE_MAX_SIZE    = GlobalConstants.getMaxImagePixelSize();   
13.    BitmapFactory.Options bfo   = new BitmapFactory.Options();
14.    bfo.inJustDecodeBounds      = true;
15.     
16.    BitmapFactory.decodeFile(strFilePath, bfo);
17.     
18.    if(bfo.outHeight * bfo.outWidth >= IMAGE_MAX_SIZE * IMAGE_MAX_SIZE)
19.    {
20.        bfo.inSampleSize = (int)Math.pow(2, (int)Math.round(Math.log(IMAGE_MAX_SIZE
21.                            / (double) Math.max(bfo.outHeight, bfo.outWidth)) / Math.log(0.5)));
22.    }
23.    bfo.inJustDecodeBounds = false;
24.    bfo.inPurgeable = true;
25.    bfo.inDither = true;
26.    
27.    final Bitmap bitmap = BitmapFactory.decodeFile(strFilePath, bfo);
28.     
29.    return bitmap;
30.}




그리고 이미지를 네트워크에서 다운로드해서 파일로 저장하는 함수입니다.

001.Bitmap downloadBitmap(String strImageURL)
002.{
003.    final HttpClient client         = (mode == Mode.NO_ASYNC_TASK) ?
004.            new DefaultHttpClient() : AndroidHttpClient.newInstance("Android");
005.             
006.    final HttpGet getRequest        = new HttpGet(strImageURL);
007.    // 가로, 세로 최대 크기 (이보다 큰 이미지가 들어올 경우 크기를 줄인다.)
008.    final int IMAGE_MAX_SIZE        = GlobalConstants.getMaxImagePixelSize();
009.    // FileCache를 위한 이미지 파일명(URL Encoding)
010.    final String strImageFilePath   = GlobalConstants.GetImageFileCachePath()
011.                                            + URLEncoder.encode(strImageURL);  
012.     
013.    try
014.    {
015.        // FileCache를 먼저 확인한 다음 원하는 이미지가 없으면 다운 받는다.
016.        if (bUseFileCache == true)
017.        {
018.            //Bitmap bitmap = BitmapFactory.decodeFile(strImageFilePath);
019.            Bitmap bitmap = SafeDecodeBitmapFile(strImageFilePath);
020.             
021.            if (bitmap != null)
022.            {
023.                return bitmap;
024.            }
025.        }
026.         
027.        HttpResponse response = client.execute(getRequest);
028.        final int statusCode = response.getStatusLine().getStatusCode();
029.         
030.        if (statusCode != HttpStatus.SC_OK)
031.        {
032.            return null;
033.        }
034. 
035.        final HttpEntity entity = response.getEntity();
036.         
037.        if (entity != null)
038.        {
039.            InputStream inputStream = null;
040.             
041.            try
042.            {
043.                inputStream = entity.getContent();
044. 
045.                if (bUseFileCache == true)
046.                {
047.                    // FlieCache 파일 개수 관리
048.                    ManageFileCache(GlobalConstants.GetImageFileCachePath());
049.                     
050.                    // Download한 이미지를 파일로 저장
051.                    File file = new File(strImageFilePath);
052.                    FileOutputStream fileOutput = new FileOutputStream(file); 
053.                     
054.                    FlushedInputStream fis = new FlushedInputStream(inputStream);
055.                    byte[] buffer = new byte[1024];  
056.                    int bufferLength = 0;
057.                     
058.                    try
059.                    {
060.                        while ((bufferLength = fis.read(buffer)) > 0)
061.                        {  
062.                            fileOutput.write(buffer, 0, bufferLength);
063.                        }
064.                         
065.                        fileOutput.flush();
066.                        fileOutput.close();
067.                         
068.                        return SafeDecodeBitmapFile(strImageFilePath);
069.                    }
070.                    catch(IOException e)
071.                    {
072.                        e.printStackTrace();
073.                    }
074.                }
075.                else
076.                {
077.                    // FileCache를 사용하지 않을 때 네트워크로부터 이미지를 다운 받는 부분
078.                    BitmapFactory.Options bfo   = new BitmapFactory.Options();
079.                    bfo.inJustDecodeBounds      = true;
080.                     
081.                    BitmapFactory.decodeStream(new FlushedInputStream(inputStream), null, bfo);
082.                     
083.                    if(bfo.outHeight * bfo.outWidth >= IMAGE_MAX_SIZE * IMAGE_MAX_SIZE)
084.                    {
085.                        bfo.inSampleSize = (int)Math.pow(2, (int)Math.round(Math.log(IMAGE_MAX_SIZE
086.                                            / (double) Math.max(bfo.outHeight, bfo.outWidth))
087.                                            / Math.log(0.5)));
088.                    }
089.                    bfo.inJustDecodeBounds = false;
090.                     
091.                    response = client.execute(getRequest);
092.                    final int nRetryStatusCode = response.getStatusLine().getStatusCode();
093.                     
094.                    if (nRetryStatusCode != HttpStatus.SC_OK)
095.                    {
096.                        return null;
097.                    }
098.                     
099.                    final HttpEntity reEntity = response.getEntity();
100.                     
101.                    if (reEntity != null)
102.                    {
103.                        InputStream reInputStream = null;
104.                         
105.                        try
106.                        {
107.                            reInputStream = reEntity.getContent();
108.                            final Bitmap imgBitmap = BitmapFactory.decodeStream(
109.                                                new FlushedInputStream(reInputStream), null, bfo);
110.                             
111.                            return imgBitmap;
112.                        }
113.                        finally
114.                        {
115.                             if (reInputStream != null)
116.                             {
117.                                 reInputStream.close();
118.                             }
119.                              
120.                             reEntity.consumeContent();
121.                        }
122.                    }
123.                }
124.            }
125.            finally
126.            {
127.                if (inputStream != null)
128.                {
129.                    inputStream.close();
130.                }
131.                 
132.                entity.consumeContent();
133.            }
134.        }
135.    }
136.    catch (IOException e)
137.    {
138.        getRequest.abort();
139.    }
140.    catch (IllegalStateException e)
141.    {
142.        getRequest.abort();
143.    }
144.    catch (Exception e)
145.    {
146.        getRequest.abort();
147.    }
148.    finally
149.    {
150.        if ((client instanceof AndroidHttpClient))
151.        {
152.            ((AndroidHttpClient)client).close();
153.        }
154.    }
155.     
156.    return null;
157.}

 


그리고 마지막으로 File Cache로 지정한 폴더에서 파일 개수가 일정 개수가 넘어갈 경우 
가장 오래된 파일부터 지우는 함수입니다.

01.private void ManageFileCache(String strFolderPath)
02.{
03.    long nLastModifiedDate = 0;
04.    File targetFile = null;
05.     
06.    while(true)
07.    {
08.        File[] arrFiles = new File(strFolderPath).listFiles();
09.         
10.        if (arrFiles.length < GlobalConstants.GetImageFileCacheSize())
11.        {
12.            break;
13.        }
14.         
15.        for (File file : arrFiles)
16.        {
17.            if (nLastModifiedDate < file.lastModified())
18.            {
19.                nLastModifiedDate = file.lastModified();
20.                targetFile = file;
21.            }
22.        }
23.         
24.        if (targetFile != null)
25.        {
26.            targetFile.delete();
27.        }
28.    }
29.}

List of Articles
번호 제목 날짜 조회 수
197 안드로이드 - 프레임레이아웃 (FrameLayout) file 2021.03.29 507
196 안드로이드 - 랠러티브 레이아웃(Relative Layout) file 2021.03.29 239
195 안드로이드 - 리니어 레이아웃 (Linear Layout) file 2021.03.29 319
194 안드로이드 스튜디오 - 코드 자동 들여쓰기 file 2021.03.29 420
193 안드로이드 스튜디오 - 필수 재정의 함수 자동 코드 추가 file 2021.03.29 194
192 안드로이드 스튜디오 - getter/setter 메소드 자동생성 file 2021.03.29 583
191 안드로이드 스튜디오 - 싱글톤 패턴 (SingleTon Pattenr) 클래스 자동 생성 file 2021.03.29 614
190 안드로이드 unescape /escape [StringEscapeUtils로 해결] file 2021.03.29 344
189 안드로이드 arrayList 를 Json으로 변환 / jsonarry file 2021.03.29 326
188 Apk manager 이용해 Decompile (디컴파일) 하기 file 2021.03.16 1619
187 안드로이드 입문 연습문제 3문항 - CheckBox, RadioButton, EditText, Spinner, 이벤트연습 file 2020.12.14 480
186 안드로이드 앱배포하기 apk 만들기 file 2020.12.14 324
185 초기화면 페이지를 만들어보자. splash 페이지 제작 file 2020.12.14 285
184 ListView 리스트뷰 연습3 - 커스텀 리스트뷰 (Custom ListView) file 2020.12.14 906
183 하이브리드앱 기본 - WebView로 웹페이지 띄우기 file 2020.12.14 1025
182 패키지명을 한꺼번에 변경하기 (Refactor) file 2020.12.14 295
181 안드로이드 스튜디오 actionbar(액션바) 사라짐 file 2020.12.14 604
180 This Handler class should be static or leaks might occur 시 해결법 2020.12.14 240
179 안드로이드에서 url 주소로 이미지 바로 불러오기 (Glide 사용) 2020.12.14 759
178 구글맵으로 GPS 현재위치 실시간 연동하기 file 2020.12.14 2436
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved