diff options
| author | Leon Scroggins III <scroggo@google.com> | 2018-01-31 14:59:29 -0500 |
|---|---|---|
| committer | Leon Scroggins III <scroggo@google.com> | 2018-01-31 15:43:49 -0500 |
| commit | 046a99ebbb90f9ecdead7b057ef99764a1d295b9 (patch) | |
| tree | 44794bba855b9d00b894db6de6ef035dfc68843c /core/java/android/widget/ImageView.java | |
| parent | ce9bcc4977a8f7eca00674dd5292f191e3ca98fa (diff) | |
Use ImageDecoder in ImageView.getDrawableFromUri
Bug: 63909536
Test: Existing CTS tests
ImageDecoder will bypass the InputStream if possible, allowing it to be
more efficient. In addition, it handles density scaling differently;
instead of using more RAM to scale the image up, it results in scaling
at draw time.
Change-Id: Ied7c0865a736f9ef0de367299264e18ccc3e0b92
Diffstat (limited to 'core/java/android/widget/ImageView.java')
| -rw-r--r-- | core/java/android/widget/ImageView.java | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/core/java/android/widget/ImageView.java b/core/java/android/widget/ImageView.java index 1dc5b44bed4f..4b951fa1824e 100644 --- a/core/java/android/widget/ImageView.java +++ b/core/java/android/widget/ImageView.java @@ -23,10 +23,12 @@ import android.annotation.TestApi; import android.content.ContentResolver; import android.content.Context; import android.content.res.ColorStateList; +import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.ColorFilter; +import android.graphics.ImageDecoder; import android.graphics.Matrix; import android.graphics.PixelFormat; import android.graphics.PorterDuff; @@ -53,7 +55,6 @@ import android.widget.RemoteViews.RemoteView; import com.android.internal.R; import java.io.IOException; -import java.io.InputStream; /** * Displays image resources, for example {@link android.graphics.Bitmap} @@ -946,21 +947,15 @@ public class ImageView extends View { } } else if (ContentResolver.SCHEME_CONTENT.equals(scheme) || ContentResolver.SCHEME_FILE.equals(scheme)) { - InputStream stream = null; try { - stream = mContext.getContentResolver().openInputStream(uri); - return Drawable.createFromResourceStream(sCompatUseCorrectStreamDensity - ? getResources() : null, null, stream, null); - } catch (Exception e) { + Resources res = sCompatUseCorrectStreamDensity ? getResources() : null; + ImageDecoder.Source src = ImageDecoder.createSource(mContext.getContentResolver(), + uri, res); + return ImageDecoder.decodeDrawable(src, (decoder, info, s) -> { + decoder.setAllocator(ImageDecoder.ALLOCATOR_SOFTWARE); + }); + } catch (IOException e) { Log.w(LOG_TAG, "Unable to open content: " + uri, e); - } finally { - if (stream != null) { - try { - stream.close(); - } catch (IOException e) { - Log.w(LOG_TAG, "Unable to close content: " + uri, e); - } - } } } else { return Drawable.createFromPath(uri.toString()); |
