summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Mateus Azis <azis@google.com> 2024-06-17 09:15:44 -0700
committer Mateus Azis <azis@google.com> 2024-06-24 17:57:21 +0000
commit446f2f6d82c8614e57e53276adb12f023154da88 (patch)
tree986afb6af921b1767d0d12f9c8d7e63ef41b8d8f
parentf8a9bb954489dc2c220e0a6d47cae4c10b9c3d7c (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.java41
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;
}