-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathGifUtils.java
66 lines (59 loc) · 1.91 KB
/
GifUtils.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.hss01248.retrofitdemo.gif;
import android.graphics.Bitmap;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by Administrator on 2016/8/8 0008.
*/
public class GifUtils {
public static String toHex(int value, int length) {
String hex = Integer.toHexString(value);
hex = hex.toUpperCase();
if (hex.length() < length) {
while (hex.length() < length)
hex = "0" + hex;
} else if (hex.length() > length) {
hex = hex.substring(hex.length() - length);
}
return hex;
}
public static byte[] streamToBytes(InputStream stream) throws IOException,
OutOfMemoryError {
byte[] buff = new byte[1024];
int read;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
while ((read = stream.read(buff)) != -1) {
bao.write(buff, 0, read);
}
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
return bao.toByteArray();
}
public static Bitmap getBitmapFromGifFile(File file,int targetWidth,int targetHeight){
GifImageDecoder gifDecoder = new GifImageDecoder();
gifDecoder.setResization(targetWidth,targetHeight);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
gifDecoder.read(fis);
return gifDecoder.getBitmap();
} catch (IOException e) {
e.printStackTrace();
return null;
}finally {
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}