|
| 1 | +package com.bumptech.glide; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | + |
| 5 | +import android.content.ContentResolver; |
| 6 | +import android.content.Context; |
| 7 | +import android.content.res.Resources; |
| 8 | +import android.graphics.Bitmap; |
| 9 | +import android.net.Uri; |
| 10 | +import androidx.test.core.app.ApplicationProvider; |
| 11 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 12 | +import com.bumptech.glide.load.model.UnitModelLoader; |
| 13 | +import com.bumptech.glide.test.ConcurrencyHelper; |
| 14 | +import com.bumptech.glide.test.ResourceIds; |
| 15 | +import com.bumptech.glide.test.TearDownGlide; |
| 16 | +import java.io.ByteArrayOutputStream; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStream; |
| 19 | +import java.nio.ByteBuffer; |
| 20 | +import java.util.Objects; |
| 21 | +import org.junit.Rule; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.rules.TestRule; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | + |
| 26 | +@RunWith(AndroidJUnit4.class) |
| 27 | +public class LargeImageTest { |
| 28 | + @Rule public final TestRule tearDownGlide = new TearDownGlide(); |
| 29 | + private final ConcurrencyHelper concurrency = new ConcurrencyHelper(); |
| 30 | + private final Context context = ApplicationProvider.getApplicationContext(); |
| 31 | + |
| 32 | + @Test |
| 33 | + public void loadLargeJpeg_asByteArray_succeeds() throws IOException { |
| 34 | + byte[] data = getLargeImageBytes(); |
| 35 | + Bitmap bitmap = concurrency.get(Glide.with(context).asBitmap().load(data).submit()); |
| 36 | + assertThat(bitmap).isNotNull(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void loadLargeJpeg_asByteBuffer_succeeds() throws IOException { |
| 41 | + // Using UnitModelLoader lets us mimic loading the ByteBuffer from some other data type, which |
| 42 | + // reduces the scope of our test. |
| 43 | + Glide.get(context) |
| 44 | + .getRegistry() |
| 45 | + .append( |
| 46 | + ByteBuffer.class, ByteBuffer.class, UnitModelLoader.Factory.<ByteBuffer>getInstance()); |
| 47 | + |
| 48 | + ByteBuffer buffer = ByteBuffer.wrap(getLargeImageBytes()); |
| 49 | + Bitmap bitmap = concurrency.get(Glide.with(context).asBitmap().load(buffer).submit()); |
| 50 | + assertThat(bitmap).isNotNull(); |
| 51 | + } |
| 52 | + |
| 53 | + private byte[] getLargeImageBytes() throws IOException { |
| 54 | + Resources resources = context.getResources(); |
| 55 | + int resourceId = ResourceIds.raw.canonical_large; |
| 56 | + Uri uri = |
| 57 | + new Uri.Builder() |
| 58 | + .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) |
| 59 | + .authority(resources.getResourcePackageName(resourceId)) |
| 60 | + .appendPath(resources.getResourceTypeName(resourceId)) |
| 61 | + .appendPath(resources.getResourceEntryName(resourceId)) |
| 62 | + .build(); |
| 63 | + |
| 64 | + InputStream is = Objects.requireNonNull(context.getContentResolver().openInputStream(uri)); |
| 65 | + ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 66 | + byte[] buffer = new byte[1024 * 1024 * 5]; |
| 67 | + int read; |
| 68 | + while ((read = is.read(buffer, 0, buffer.length)) != -1) { |
| 69 | + os.write(buffer, 0, read); |
| 70 | + } |
| 71 | + return os.toByteArray(); |
| 72 | + } |
| 73 | +} |
0 commit comments