개발은 하는건가..

파일 사이즈가 큰 Gif 파일에서 Preview 용 Bmp 추출하기 본문

Java, Android

파일 사이즈가 큰 Gif 파일에서 Preview 용 Bmp 추출하기

수동애비 2018. 11. 2. 10:13
반응형

GifDecoder.java


Gif 에서 Bitmap 추출이 필요할 때 BitmapFactory 로는 10~30MB 에 달하는 에니메이션 GIF 파일은 Decoding 이 되지 않았습니다.

물론 Glide 같은 라이브러리 써도 됩니다만  그건 상황에 따라 다르니.. 

암튼 누군가 친절하게 만들어논 GifDecoder 클래스를 이용해서 아래와 같이 했더니 Bitmap 이 잘 추출되었습니다. 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
private Bitmap getGifPreviewFrame(File f) {        
        if (f == null || f.exists() == false || f.length() < 32) {
            return null;
        }
                
        GifDecoder gd = new GifDecoder();
        Bitmap bmp = null;
        FileInputStream fis = null;
        
        try {
            fis = new FileInputStream(f);
            
            if (fis != null) {
                if (gd.read(fis, fis.available()) == 0) {
                    final int frameCount = gd.getFrameCount();
    
                    if (frameCount > 0) {
                        gd.advance();
                        bmp = Bitmap.createBitmap(gd.getNextFrame());
                    }                
                    
                }
            }
        } 
        catch (IOException ie) {
            ie.printStackTrace();
        } 
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {                    
                    e.printStackTrace();
                }
            }
        }
        
        gd = null;
        System.gc();
        return bmp;
    }
cs


Comments