Skip to content

Commit 853c687

Browse files
committed
Retry failed Bitmap decodes with a null inBitmap if we can reset the InputStream
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=157882288
1 parent 8dfb63e commit 853c687

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

library/src/main/java/com/bumptech/glide/load/resource/bitmap/Downsampler.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private Bitmap decodeFromWrappedStreams(InputStream is,
188188
DecodeFormat decodeFormat, int requestedWidth, int requestedHeight,
189189
boolean fixBitmapToRequestedDimensions, DecodeCallbacks callbacks) throws IOException {
190190

191-
int[] sourceDimensions = getDimensions(is, options, callbacks);
191+
int[] sourceDimensions = getDimensions(is, options, callbacks, bitmapPool);
192192
int sourceWidth = sourceDimensions[0];
193193
int sourceHeight = sourceDimensions[1];
194194
String sourceMimeType = options.outMimeType;
@@ -240,7 +240,7 @@ && shouldUsePool(is)) {
240240
setInBitmap(options, bitmapPool, expectedWidth, expectedHeight);
241241
}
242242
}
243-
Bitmap downsampled = decodeStream(is, options, callbacks);
243+
Bitmap downsampled = decodeStream(is, options, callbacks, bitmapPool);
244244
callbacks.onDecodeComplete(bitmapPool, downsampled);
245245

246246
if (Log.isLoggable(TAG, Log.VERBOSE)) {
@@ -394,15 +394,15 @@ private Bitmap.Config getConfig(InputStream is, DecodeFormat format) throws IOEx
394394
* @return an array containing the dimensions of the image in the form {width, height}.
395395
*/
396396
private static int[] getDimensions(InputStream is, BitmapFactory.Options options,
397-
DecodeCallbacks decodeCallbacks) throws IOException {
397+
DecodeCallbacks decodeCallbacks, BitmapPool bitmapPool) throws IOException {
398398
options.inJustDecodeBounds = true;
399-
decodeStream(is, options, decodeCallbacks);
399+
decodeStream(is, options, decodeCallbacks, bitmapPool);
400400
options.inJustDecodeBounds = false;
401401
return new int[] { options.outWidth, options.outHeight };
402402
}
403403

404404
private static Bitmap decodeStream(InputStream is, BitmapFactory.Options options,
405-
DecodeCallbacks callbacks) throws IOException {
405+
DecodeCallbacks callbacks, BitmapPool bitmapPool) throws IOException {
406406
if (options.inJustDecodeBounds) {
407407
is.mark(MARK_POSITION);
408408
} else {
@@ -423,7 +423,23 @@ private static Bitmap decodeStream(InputStream is, BitmapFactory.Options options
423423
try {
424424
result = BitmapFactory.decodeStream(is, null, options);
425425
} catch (IllegalArgumentException e) {
426-
throw newIoExceptionForInBitmapAssertion(e, sourceWidth, sourceHeight, outMimeType, options);
426+
IOException bitmapAssertionException =
427+
newIoExceptionForInBitmapAssertion(e, sourceWidth, sourceHeight, outMimeType, options);
428+
if (Log.isLoggable(TAG, Log.DEBUG)) {
429+
Log.d(TAG, "Failed to decode with inBitmap, trying again without Bitmap re-use",
430+
bitmapAssertionException);
431+
}
432+
if (options.inBitmap != null) {
433+
try {
434+
is.reset();
435+
bitmapPool.put(options.inBitmap);
436+
options.inBitmap = null;
437+
return decodeStream(is, options, callbacks, bitmapPool);
438+
} catch (IOException resetException) {
439+
throw bitmapAssertionException;
440+
}
441+
}
442+
throw bitmapAssertionException;
427443
} finally {
428444
TransformationUtils.getBitmapDrawableLock().unlock();
429445
}

0 commit comments

Comments
 (0)