Skip to content

Commit

Permalink
bitmap decode过程加锁, 不允许多个线程同时decode.
Browse files Browse the repository at this point in the history
  • Loading branch information
wyouflf committed Aug 4, 2014
1 parent ba8ee27 commit d720558
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ private Bitmap decodeBitmapMeta(BitmapMeta bitmapMeta, BitmapDisplayConfig confi
return bitmap;
}

private Bitmap rotateBitmapIfNeeded(String uri, BitmapDisplayConfig config, Bitmap bitmap) {
private synchronized Bitmap rotateBitmapIfNeeded(String uri, BitmapDisplayConfig config, Bitmap bitmap) {
Bitmap result = bitmap;
if (config != null && config.isAutoRotation()) {
File bitmapFile = this.getBitmapFileFromDiskCache(uri);
Expand Down
202 changes: 110 additions & 92 deletions library/src/com/lidroid/xutils/bitmap/core/BitmapDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,130 +24,148 @@

public class BitmapDecoder {

private static final Object lock = new Object();

private BitmapDecoder() {
}

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, BitmapSize maxSize, Bitmap.Config config) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPurgeable = true;
options.inInputShareable = true;
BitmapFactory.decodeResource(res, resId, options);
options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight());
options.inJustDecodeBounds = false;
if (config != null) {
options.inPreferredConfig = config;
}
try {
return BitmapFactory.decodeResource(res, resId, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
synchronized (lock) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPurgeable = true;
options.inInputShareable = true;
BitmapFactory.decodeResource(res, resId, options);
options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight());
options.inJustDecodeBounds = false;
if (config != null) {
options.inPreferredConfig = config;
}
try {
return BitmapFactory.decodeResource(res, resId, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
}
}
}

public static Bitmap decodeSampledBitmapFromFile(String filename, BitmapSize maxSize, Bitmap.Config config) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPurgeable = true;
options.inInputShareable = true;
BitmapFactory.decodeFile(filename, options);
options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight());
options.inJustDecodeBounds = false;
if (config != null) {
options.inPreferredConfig = config;
}
try {
return BitmapFactory.decodeFile(filename, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
synchronized (lock) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPurgeable = true;
options.inInputShareable = true;
BitmapFactory.decodeFile(filename, options);
options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight());
options.inJustDecodeBounds = false;
if (config != null) {
options.inPreferredConfig = config;
}
try {
return BitmapFactory.decodeFile(filename, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
}
}
}

public static Bitmap decodeSampledBitmapFromDescriptor(FileDescriptor fileDescriptor, BitmapSize maxSize, Bitmap.Config config) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPurgeable = true;
options.inInputShareable = true;
BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight());
options.inJustDecodeBounds = false;
if (config != null) {
options.inPreferredConfig = config;
}
try {
return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
synchronized (lock) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPurgeable = true;
options.inInputShareable = true;
BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight());
options.inJustDecodeBounds = false;
if (config != null) {
options.inPreferredConfig = config;
}
try {
return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
}
}
}

public static Bitmap decodeSampledBitmapFromByteArray(byte[] data, BitmapSize maxSize, Bitmap.Config config) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPurgeable = true;
options.inInputShareable = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight());
options.inJustDecodeBounds = false;
if (config != null) {
options.inPreferredConfig = config;
}
try {
return BitmapFactory.decodeByteArray(data, 0, data.length, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
synchronized (lock) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPurgeable = true;
options.inInputShareable = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
options.inSampleSize = calculateInSampleSize(options, maxSize.getWidth(), maxSize.getHeight());
options.inJustDecodeBounds = false;
if (config != null) {
options.inPreferredConfig = config;
}
try {
return BitmapFactory.decodeByteArray(data, 0, data.length, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
}
}
}

public static Bitmap decodeResource(Resources res, int resId) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
try {
return BitmapFactory.decodeResource(res, resId, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
synchronized (lock) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
try {
return BitmapFactory.decodeResource(res, resId, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
}
}
}

public static Bitmap decodeFile(String filename) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
try {
return BitmapFactory.decodeFile(filename, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
synchronized (lock) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
try {
return BitmapFactory.decodeFile(filename, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
}
}
}

public static Bitmap decodeFileDescriptor(FileDescriptor fileDescriptor) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
try {
return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
synchronized (lock) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
try {
return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
}
}
}

public static Bitmap decodeByteArray(byte[] data) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
try {
return BitmapFactory.decodeByteArray(data, 0, data.length, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
synchronized (lock) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
try {
return BitmapFactory.decodeByteArray(data, 0, data.length, options);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return null;
}
}
}

Expand Down

0 comments on commit d720558

Please sign in to comment.