diff options
| author | 2024-06-17 09:15:44 -0700 | |
|---|---|---|
| committer | 2024-06-24 17:57:21 +0000 | |
| commit | 446f2f6d82c8614e57e53276adb12f023154da88 (patch) | |
| tree | 986afb6af921b1767d0d12f9c8d7e63ef41b8d8f | |
| parent | f8a9bb954489dc2c220e0a6d47cae4c10b9c3d7c (diff) | |
Close InputStreams when loading icons.
BitmapFactory.decodeStream does not claim to close the provided input
stream. It's currently leaking and triggering a CloseGuard exception.
Bug: 347694023
Test: flashed and "adb logcat *:S StrictMode"
Flag: EXEMPT bugfix
Change-Id: I377f288e678cc0afbf1735371d995ce38c7d910b
| -rw-r--r-- | graphics/java/android/graphics/drawable/Icon.java | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/graphics/java/android/graphics/drawable/Icon.java b/graphics/java/android/graphics/drawable/Icon.java index c3aaf983711d..f8cb050fb360 100644 --- a/graphics/java/android/graphics/drawable/Icon.java +++ b/graphics/java/android/graphics/drawable/Icon.java @@ -494,30 +494,37 @@ public final class Icon implements Parcelable { BitmapFactory.decodeByteArray(getDataBytes(), getDataOffset(), getDataLength()))); case TYPE_URI: - InputStream is = getUriInputStream(context); - if (is != null) { - final Bitmap bitmap = BitmapFactory.decodeStream(is); - if (bitmap == null) { - Log.w(TAG, "Unable to decode image from URI: " + getUriString()); - if (iconLoadDrawableReturnNullWhenUriDecodeFails()) { - return null; + try (InputStream is = getUriInputStream(context)) { + if (is != null) { + final Bitmap bitmap = BitmapFactory.decodeStream(is); + if (bitmap == null) { + Log.w(TAG, "Unable to decode image from URI: " + getUriString()); + if (iconLoadDrawableReturnNullWhenUriDecodeFails()) { + return null; + } } + return new BitmapDrawable(context.getResources(), fixMaxBitmapSize(bitmap)); } - return new BitmapDrawable(context.getResources(), fixMaxBitmapSize(bitmap)); + } catch (IOException e) { + throw new IllegalStateException(e); } break; case TYPE_URI_ADAPTIVE_BITMAP: - is = getUriInputStream(context); - if (is != null) { - final Bitmap bitmap = BitmapFactory.decodeStream(is); - if (bitmap == null) { - Log.w(TAG, "Unable to decode image from URI: " + getUriString()); - if (iconLoadDrawableReturnNullWhenUriDecodeFails()) { - return null; + try (InputStream is = getUriInputStream(context)) { + if (is != null) { + final Bitmap bitmap = BitmapFactory.decodeStream(is); + if (bitmap == null) { + Log.w(TAG, "Unable to decode image from URI: " + getUriString()); + if (iconLoadDrawableReturnNullWhenUriDecodeFails()) { + return null; + } } + return new AdaptiveIconDrawable( + null, new BitmapDrawable(context.getResources(), + fixMaxBitmapSize(bitmap))); } - return new AdaptiveIconDrawable(null, new BitmapDrawable(context.getResources(), - fixMaxBitmapSize(bitmap))); + } catch (IOException e) { + throw new IllegalStateException(e); } break; } |