|
24 | 24 |
|
25 | 25 | public class BitmapDecoder {
|
26 | 26 |
|
| 27 | + private static final Object lock = new Object(); |
| 28 | + |
27 | 29 | private BitmapDecoder() {
|
28 | 30 | }
|
29 | 31 |
|
30 | 32 | public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, BitmapSize maxSize, Bitmap.Config config) {
|
31 |
| - final BitmapFactory.Options options = new BitmapFactory.Options(); |
32 |
| - options.inJustDecodeBounds = true; |
33 |
| - options.inPurgeable = true; |
34 |
| - options.inInputShareable = true; |
35 |
| - BitmapFactory.decodeResource(res, resId, options); |
36 |
| - options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight()); |
37 |
| - options.inJustDecodeBounds = false; |
38 |
| - if (config != null) { |
39 |
| - options.inPreferredConfig = config; |
40 |
| - } |
41 |
| - try { |
42 |
| - return BitmapFactory.decodeResource(res, resId, options); |
43 |
| - } catch (Throwable e) { |
44 |
| - LogUtils.e(e.getMessage(), e); |
45 |
| - return null; |
| 33 | + synchronized (lock) { |
| 34 | + final BitmapFactory.Options options = new BitmapFactory.Options(); |
| 35 | + options.inJustDecodeBounds = true; |
| 36 | + options.inPurgeable = true; |
| 37 | + options.inInputShareable = true; |
| 38 | + BitmapFactory.decodeResource(res, resId, options); |
| 39 | + options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight()); |
| 40 | + options.inJustDecodeBounds = false; |
| 41 | + if (config != null) { |
| 42 | + options.inPreferredConfig = config; |
| 43 | + } |
| 44 | + try { |
| 45 | + return BitmapFactory.decodeResource(res, resId, options); |
| 46 | + } catch (Throwable e) { |
| 47 | + LogUtils.e(e.getMessage(), e); |
| 48 | + return null; |
| 49 | + } |
46 | 50 | }
|
47 | 51 | }
|
48 | 52 |
|
49 | 53 | public static Bitmap decodeSampledBitmapFromFile(String filename, BitmapSize maxSize, Bitmap.Config config) {
|
50 |
| - final BitmapFactory.Options options = new BitmapFactory.Options(); |
51 |
| - options.inJustDecodeBounds = true; |
52 |
| - options.inPurgeable = true; |
53 |
| - options.inInputShareable = true; |
54 |
| - BitmapFactory.decodeFile(filename, options); |
55 |
| - options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight()); |
56 |
| - options.inJustDecodeBounds = false; |
57 |
| - if (config != null) { |
58 |
| - options.inPreferredConfig = config; |
59 |
| - } |
60 |
| - try { |
61 |
| - return BitmapFactory.decodeFile(filename, options); |
62 |
| - } catch (Throwable e) { |
63 |
| - LogUtils.e(e.getMessage(), e); |
64 |
| - return null; |
| 54 | + synchronized (lock) { |
| 55 | + final BitmapFactory.Options options = new BitmapFactory.Options(); |
| 56 | + options.inJustDecodeBounds = true; |
| 57 | + options.inPurgeable = true; |
| 58 | + options.inInputShareable = true; |
| 59 | + BitmapFactory.decodeFile(filename, options); |
| 60 | + options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight()); |
| 61 | + options.inJustDecodeBounds = false; |
| 62 | + if (config != null) { |
| 63 | + options.inPreferredConfig = config; |
| 64 | + } |
| 65 | + try { |
| 66 | + return BitmapFactory.decodeFile(filename, options); |
| 67 | + } catch (Throwable e) { |
| 68 | + LogUtils.e(e.getMessage(), e); |
| 69 | + return null; |
| 70 | + } |
65 | 71 | }
|
66 | 72 | }
|
67 | 73 |
|
68 | 74 | public static Bitmap decodeSampledBitmapFromDescriptor(FileDescriptor fileDescriptor, BitmapSize maxSize, Bitmap.Config config) {
|
69 |
| - final BitmapFactory.Options options = new BitmapFactory.Options(); |
70 |
| - options.inJustDecodeBounds = true; |
71 |
| - options.inPurgeable = true; |
72 |
| - options.inInputShareable = true; |
73 |
| - BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); |
74 |
| - options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight()); |
75 |
| - options.inJustDecodeBounds = false; |
76 |
| - if (config != null) { |
77 |
| - options.inPreferredConfig = config; |
78 |
| - } |
79 |
| - try { |
80 |
| - return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); |
81 |
| - } catch (Throwable e) { |
82 |
| - LogUtils.e(e.getMessage(), e); |
83 |
| - return null; |
| 75 | + synchronized (lock) { |
| 76 | + final BitmapFactory.Options options = new BitmapFactory.Options(); |
| 77 | + options.inJustDecodeBounds = true; |
| 78 | + options.inPurgeable = true; |
| 79 | + options.inInputShareable = true; |
| 80 | + BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); |
| 81 | + options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight()); |
| 82 | + options.inJustDecodeBounds = false; |
| 83 | + if (config != null) { |
| 84 | + options.inPreferredConfig = config; |
| 85 | + } |
| 86 | + try { |
| 87 | + return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); |
| 88 | + } catch (Throwable e) { |
| 89 | + LogUtils.e(e.getMessage(), e); |
| 90 | + return null; |
| 91 | + } |
84 | 92 | }
|
85 | 93 | }
|
86 | 94 |
|
87 | 95 | public static Bitmap decodeSampledBitmapFromByteArray(byte[] data, BitmapSize maxSize, Bitmap.Config config) {
|
88 |
| - final BitmapFactory.Options options = new BitmapFactory.Options(); |
89 |
| - options.inJustDecodeBounds = true; |
90 |
| - options.inPurgeable = true; |
91 |
| - options.inInputShareable = true; |
92 |
| - BitmapFactory.decodeByteArray(data, 0, data.length, options); |
93 |
| - options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight()); |
94 |
| - options.inJustDecodeBounds = false; |
95 |
| - if (config != null) { |
96 |
| - options.inPreferredConfig = config; |
97 |
| - } |
98 |
| - try { |
99 |
| - return BitmapFactory.decodeByteArray(data, 0, data.length, options); |
100 |
| - } catch (Throwable e) { |
101 |
| - LogUtils.e(e.getMessage(), e); |
102 |
| - return null; |
| 96 | + synchronized (lock) { |
| 97 | + final BitmapFactory.Options options = new BitmapFactory.Options(); |
| 98 | + options.inJustDecodeBounds = true; |
| 99 | + options.inPurgeable = true; |
| 100 | + options.inInputShareable = true; |
| 101 | + BitmapFactory.decodeByteArray(data, 0, data.length, options); |
| 102 | + options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight()); |
| 103 | + options.inJustDecodeBounds = false; |
| 104 | + if (config != null) { |
| 105 | + options.inPreferredConfig = config; |
| 106 | + } |
| 107 | + try { |
| 108 | + return BitmapFactory.decodeByteArray(data, 0, data.length, options); |
| 109 | + } catch (Throwable e) { |
| 110 | + LogUtils.e(e.getMessage(), e); |
| 111 | + return null; |
| 112 | + } |
103 | 113 | }
|
104 | 114 | }
|
105 | 115 |
|
106 | 116 | public static Bitmap decodeResource(Resources res, int resId) {
|
107 |
| - final BitmapFactory.Options options = new BitmapFactory.Options(); |
108 |
| - options.inPurgeable = true; |
109 |
| - options.inInputShareable = true; |
110 |
| - try { |
111 |
| - return BitmapFactory.decodeResource(res, resId, options); |
112 |
| - } catch (Throwable e) { |
113 |
| - LogUtils.e(e.getMessage(), e); |
114 |
| - return null; |
| 117 | + synchronized (lock) { |
| 118 | + final BitmapFactory.Options options = new BitmapFactory.Options(); |
| 119 | + options.inPurgeable = true; |
| 120 | + options.inInputShareable = true; |
| 121 | + try { |
| 122 | + return BitmapFactory.decodeResource(res, resId, options); |
| 123 | + } catch (Throwable e) { |
| 124 | + LogUtils.e(e.getMessage(), e); |
| 125 | + return null; |
| 126 | + } |
115 | 127 | }
|
116 | 128 | }
|
117 | 129 |
|
118 | 130 | public static Bitmap decodeFile(String filename) {
|
119 |
| - final BitmapFactory.Options options = new BitmapFactory.Options(); |
120 |
| - options.inPurgeable = true; |
121 |
| - options.inInputShareable = true; |
122 |
| - try { |
123 |
| - return BitmapFactory.decodeFile(filename, options); |
124 |
| - } catch (Throwable e) { |
125 |
| - LogUtils.e(e.getMessage(), e); |
126 |
| - return null; |
| 131 | + synchronized (lock) { |
| 132 | + final BitmapFactory.Options options = new BitmapFactory.Options(); |
| 133 | + options.inPurgeable = true; |
| 134 | + options.inInputShareable = true; |
| 135 | + try { |
| 136 | + return BitmapFactory.decodeFile(filename, options); |
| 137 | + } catch (Throwable e) { |
| 138 | + LogUtils.e(e.getMessage(), e); |
| 139 | + return null; |
| 140 | + } |
127 | 141 | }
|
128 | 142 | }
|
129 | 143 |
|
130 | 144 | public static Bitmap decodeFileDescriptor(FileDescriptor fileDescriptor) {
|
131 |
| - final BitmapFactory.Options options = new BitmapFactory.Options(); |
132 |
| - options.inPurgeable = true; |
133 |
| - options.inInputShareable = true; |
134 |
| - try { |
135 |
| - return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); |
136 |
| - } catch (Throwable e) { |
137 |
| - LogUtils.e(e.getMessage(), e); |
138 |
| - return null; |
| 145 | + synchronized (lock) { |
| 146 | + final BitmapFactory.Options options = new BitmapFactory.Options(); |
| 147 | + options.inPurgeable = true; |
| 148 | + options.inInputShareable = true; |
| 149 | + try { |
| 150 | + return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); |
| 151 | + } catch (Throwable e) { |
| 152 | + LogUtils.e(e.getMessage(), e); |
| 153 | + return null; |
| 154 | + } |
139 | 155 | }
|
140 | 156 | }
|
141 | 157 |
|
142 | 158 | public static Bitmap decodeByteArray(byte[] data) {
|
143 |
| - final BitmapFactory.Options options = new BitmapFactory.Options(); |
144 |
| - options.inPurgeable = true; |
145 |
| - options.inInputShareable = true; |
146 |
| - try { |
147 |
| - return BitmapFactory.decodeByteArray(data, 0, data.length, options); |
148 |
| - } catch (Throwable e) { |
149 |
| - LogUtils.e(e.getMessage(), e); |
150 |
| - return null; |
| 159 | + synchronized (lock) { |
| 160 | + final BitmapFactory.Options options = new BitmapFactory.Options(); |
| 161 | + options.inPurgeable = true; |
| 162 | + options.inInputShareable = true; |
| 163 | + try { |
| 164 | + return BitmapFactory.decodeByteArray(data, 0, data.length, options); |
| 165 | + } catch (Throwable e) { |
| 166 | + LogUtils.e(e.getMessage(), e); |
| 167 | + return null; |
| 168 | + } |
151 | 169 | }
|
152 | 170 | }
|
153 | 171 |
|
|
0 commit comments