Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions library/src/main/java/com/bumptech/glide/RegistryFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.bumptech.glide.load.resource.bitmap.ResourceBitmapDecoder;
import com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder;
import com.bumptech.glide.load.resource.bitmap.UnitBitmapDecoder;
import com.bumptech.glide.load.resource.bitmap.UriBitmapImageDecoderResourceDecoder;
import com.bumptech.glide.load.resource.bitmap.VideoDecoder;
import com.bumptech.glide.load.resource.bytes.ByteBufferRewinder;
import com.bumptech.glide.load.resource.drawable.AnimatedImageDecoder;
Expand Down Expand Up @@ -160,7 +161,8 @@ private static void initializeDefaults(

ResourceDecoder<ByteBuffer, Bitmap> byteBufferBitmapDecoder;
ResourceDecoder<InputStream, Bitmap> streamBitmapDecoder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
ResourceDecoder<Uri, Bitmap> uriBitmapDecoder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
&& experiments.isEnabled(EnableImageDecoderForBitmaps.class)) {
streamBitmapDecoder =
new InputStreamBitmapImageDecoderResourceDecoder(
Expand All @@ -170,6 +172,7 @@ private static void initializeDefaults(
experiments.isEnabled(
GlideBuilder.UseArrayPoolForImageDecoderByteBufferAllocation.class));
byteBufferBitmapDecoder = new ByteBufferBitmapImageDecoderResourceDecoder();
uriBitmapDecoder = new UriBitmapImageDecoderResourceDecoder(context);
} else {
byteBufferBitmapDecoder = new ByteBufferBitmapDecoder(downsampler);
streamBitmapDecoder = new StreamBitmapDecoder(downsampler, arrayPool);
Expand Down Expand Up @@ -204,6 +207,11 @@ private static void initializeDefaults(
.append(Registry.BUCKET_BITMAP, ByteBuffer.class, Bitmap.class, byteBufferBitmapDecoder)
.append(Registry.BUCKET_BITMAP, InputStream.class, Bitmap.class, streamBitmapDecoder);

if (uriBitmapDecoder != null) {
registry.prepend(Uri.class, Bitmap.class, uriBitmapDecoder);
registry.prepend(Uri.class, Uri.class, UnitModelLoader.Factory.<Uri>getInstance());
}

if (ParcelFileDescriptorRewinder.isSupported()) {
registry.append(
Registry.BUCKET_BITMAP,
Expand Down Expand Up @@ -244,7 +252,16 @@ private static void initializeDefaults(
Registry.BUCKET_BITMAP_DRAWABLE,
ParcelFileDescriptor.class,
BitmapDrawable.class,
new BitmapDrawableDecoder<>(resources, parcelFileDescriptorVideoDecoder))
new BitmapDrawableDecoder<>(resources, parcelFileDescriptorVideoDecoder));

if (uriBitmapDecoder != null) {
registry.prepend(
Uri.class,
BitmapDrawable.class,
new BitmapDrawableDecoder<>(resources, uriBitmapDecoder));
}

registry
.append(BitmapDrawable.class, new BitmapDrawableEncoder(bitmapPool, bitmapEncoder))
/* GIFs */
.append(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.bumptech.glide.load.resource.bitmap;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.ImageDecoder;
import android.graphics.ImageDecoder.Source;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.engine.Resource;
import java.io.IOException;

/** Decodes {@link Bitmap}s from {@link Uri}s using {@link ImageDecoder}. */
@RequiresApi(Build.VERSION_CODES.P)
public final class UriBitmapImageDecoderResourceDecoder implements ResourceDecoder<Uri, Bitmap> {
private static final String TAG = "UriBitmapDecoder";
private final Context context;
private final BitmapImageDecoderResourceDecoder wrapped = new BitmapImageDecoderResourceDecoder();

public UriBitmapImageDecoderResourceDecoder(@NonNull Context context) {
this.context = context.getApplicationContext();
}

@Override
public boolean handles(@NonNull Uri uri, @NonNull Options options) throws IOException {
String scheme = uri.getScheme();
boolean isSupportedScheme =
ContentResolver.SCHEME_CONTENT.equals(scheme)
|| ContentResolver.SCHEME_FILE.equals(scheme)
|| ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme);
if (!isSupportedScheme) {
return false;
}
String mimeType = context.getContentResolver().getType(uri);
if (mimeType != null && mimeType.equals("image/gif")) {
return false;
}
return true;
}

@Override
public Resource<Bitmap> decode(@NonNull Uri uri, int width, int height, @NonNull Options options)
throws IOException {
Source source = ImageDecoder.createSource(context.getContentResolver(), uri);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
String mimeType = context.getContentResolver().getType(uri);
Log.v(
TAG, "decoding " + uri + ", mimeType: " + mimeType + ", [" + width + ", " + height + "]");
}
return wrapped.decode(source, width, height, options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.bumptech.glide.load.resource.bitmap;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.bumptech.glide.load.Options;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

@RunWith(AndroidJUnit4.class)
@Config(sdk = Build.VERSION_CODES.Q)
public final class UriBitmapImageDecoderResourceDecoderTest {

private Context context;
private UriBitmapImageDecoderResourceDecoder decoder;
private Options options;

@Before
public void setUp() {
context = ApplicationProvider.getApplicationContext();
decoder = new UriBitmapImageDecoderResourceDecoder(context);
options = new Options();
}

@Test
public void handles_returnsTrueForUri() throws IOException {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
Uri uri =
context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
assertThat(decoder.handles(uri, options)).isTrue();
}

@Test
public void decode_withNonExistentUri_throwsIOException() {
Uri uri = Uri.parse("file:///non-existent-file.png");
assertThrows(
IOException.class, () -> decoder.decode(uri, /* width= */ 100, /* height= */ 100, options));
}

@Test
public void handles_returnsTrueForFileUri() throws IOException {
Uri uri = Uri.parse("file:///path/to/image.png");
assertThat(decoder.handles(uri, options)).isTrue();
}

@Test
public void handles_returnsTrueForResourceUri() throws IOException {
Uri uri = Uri.parse("android.resource://com.bumptech.glide.test/raw/image");
assertThat(decoder.handles(uri, options)).isTrue();
}

@Test
public void handles_returnsFalseForHttpUri() throws IOException {
Uri uri = Uri.parse("http://example.com/image.png");
assertThat(decoder.handles(uri, options)).isFalse();
}

@Test
public void handles_returnsFalseForGifUri() throws IOException {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.MIME_TYPE, "image/gif");
Uri uri =
context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
assertThat(decoder.handles(uri, options)).isFalse();
}
}
Loading
Loading