Skip to content

Commit d720558

Browse files
committed
bitmap decode过程加锁, 不允许多个线程同时decode.
1 parent ba8ee27 commit d720558

File tree

2 files changed

+111
-93
lines changed

2 files changed

+111
-93
lines changed

library/src/com/lidroid/xutils/bitmap/core/BitmapCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ private Bitmap decodeBitmapMeta(BitmapMeta bitmapMeta, BitmapDisplayConfig confi
422422
return bitmap;
423423
}
424424

425-
private Bitmap rotateBitmapIfNeeded(String uri, BitmapDisplayConfig config, Bitmap bitmap) {
425+
private synchronized Bitmap rotateBitmapIfNeeded(String uri, BitmapDisplayConfig config, Bitmap bitmap) {
426426
Bitmap result = bitmap;
427427
if (config != null && config.isAutoRotation()) {
428428
File bitmapFile = this.getBitmapFileFromDiskCache(uri);

library/src/com/lidroid/xutils/bitmap/core/BitmapDecoder.java

Lines changed: 110 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -24,130 +24,148 @@
2424

2525
public class BitmapDecoder {
2626

27+
private static final Object lock = new Object();
28+
2729
private BitmapDecoder() {
2830
}
2931

3032
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+
}
4650
}
4751
}
4852

4953
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+
}
6571
}
6672
}
6773

6874
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+
}
8492
}
8593
}
8694

8795
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+
}
103113
}
104114
}
105115

106116
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+
}
115127
}
116128
}
117129

118130
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+
}
127141
}
128142
}
129143

130144
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+
}
139155
}
140156
}
141157

142158
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+
}
151169
}
152170
}
153171

0 commit comments

Comments
 (0)