/** * Attempts to always load the resource using any registered {@link * com.bumptech.glide.load.ResourceDecoder}s that can decode any subclass of {@link Drawable}. * * <p> By default, may return either a {@link android.graphics.drawable.BitmapDrawable} or {@link * GifDrawable}, but if additional decoders are registered for other {@link Drawable} subclasses, * any of those subclasses may also be returned. </p> * * @return A new request builder for loading a {@link Drawable}. */ @NonNull @CheckResult public RequestBuilder<Drawable> asDrawable() { return as(Drawable.class); }
/** * Attempts to always load the resource as a {@link android.graphics.Bitmap}, even if it could * actually be animated. * * @return A new request builder for loading a {@link android.graphics.Bitmap} */ @NonNull @CheckResult public RequestBuilder<Bitmap> asBitmap() { return as(Bitmap.class).apply(DECODE_TYPE_BITMAP); }
/** * Attempts always load the resource into the cache and return the {@link File} containing the * cached source data. * * <p>This method is designed to work for remote data that is or will be cached using {@link * com.bumptech.glide.load.engine.DiskCacheStrategy#DATA}. As a result, specifying a * {@link com.bumptech.glide.load.engine.DiskCacheStrategy} on this request is generally not * recommended. * * @return A new request builder for downloading content to cache and returning the cache File. */ @NonNull @CheckResult public RequestBuilder<File> downloadOnly() { return as(File.class).apply(DOWNLOAD_ONLY_OPTIONS); }
/** * Attempts to always load the resource as a * {@link com.bumptech.glide.load.resource.gif.GifDrawable}. * * <p> If the underlying data is not a GIF, this will fail. As a result, this should only be used * if the model represents an animated GIF and the caller wants to interact with the GifDrawable * directly. Normally using just {@link #asDrawable()} is sufficient because it will determine * whether or not the given data represents an animated GIF and return the appropriate {@link * Drawable}, animated or not, automatically. </p> * * @return A new request builder for loading a * {@link com.bumptech.glide.load.resource.gif.GifDrawable}. */ @NonNull @CheckResult public RequestBuilder<GifDrawable> asGif() { return as(GifDrawable.class).apply(DECODE_TYPE_GIF); }
/** * Attempts to always load a {@link File} containing the resource, either using a file path * obtained from the media store (for local images/videos), or using Glide's disk cache * (for remote images/videos). * * <p>For remote content, prefer {@link #downloadOnly()}. * * @return A new request builder for obtaining File paths to content. */ @NonNull @CheckResult public RequestBuilder<File> asFile() { return as(File.class).apply(skipMemoryCacheOf(true)); }
@Test public void loadBitmapDrawable_asBytes_providesBytesOfBitmap() { Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical); byte[] data = concurrency.get( Glide.with(context) .as(byte[].class) .load(new BitmapDrawable(context.getResources(), bitmap)) .submit()); assertThat(data).isNotNull(); assertThat(BitmapFactory.decodeByteArray(data, 0, data.length)).isNotNull(); }
@Test public void loadBitmap_asBytes_providesBytesOfBitmap() { Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical); byte[] data = concurrency.get( Glide.with(context) .as(byte[].class) .load(bitmap) .submit()); assertThat(data).isNotNull(); assertThat(BitmapFactory.decodeByteArray(data, 0, data.length)).isNotNull(); }
@Test public void loadVideoResourceId_asBytes_providesBytesOfFrame() { byte[] data = concurrency.get( Glide.with(context) .as(byte[].class) .load(ResourceIds.raw.video) .submit()); assertThat(data).isNotNull(); assertThat(BitmapFactory.decodeByteArray(data, 0, data.length)).isNotNull(); }
@Test public void loadVideoFilePath_asBytes_providesByteOfFrame() throws IOException { byte[] data = concurrency.get( Glide.with(context) .as(byte[].class) .load(writeVideoToFile().getAbsolutePath()) .submit()); assertThat(data).isNotNull(); assertThat(BitmapFactory.decodeByteArray(data, 0, data.length)).isNotNull(); }
@Test public void loadImageResourceId_asBytes_providesBytesOfBitmap() { byte[] data = concurrency.get( Glide.with(context) .as(byte[].class) .load(ResourceIds.raw.canonical) .submit()); assertThat(data).isNotNull(); assertThat(BitmapFactory.decodeByteArray(data, 0, data.length)).isNotNull(); }
@Test public void loadVideoFileUri_asBytes_providesByteOfFrame() throws IOException { byte[] data = concurrency.get( Glide.with(context) .as(byte[].class) .load(Uri.fromFile(writeVideoToFile())) .submit()); assertThat(data).isNotNull(); assertThat(BitmapFactory.decodeByteArray(data, 0, data.length)).isNotNull(); }
@Test public void loadVideoFile_asBytes_providesByteOfFrame() throws IOException { byte[] data = concurrency.get( Glide.with(context) .as(byte[].class) .load(writeVideoToFile()) .submit()); assertThat(data).isNotNull(); assertThat(BitmapFactory.decodeByteArray(data, 0, data.length)).isNotNull(); }
@Test public void testToBytesOption() { Uri uri = Uri.parse("content://something/else"); mockUri(uri); requestManager.as(byte[].class).apply(decodeTypeOf(Bitmap.class)).load(uri).into(target); verify(target).onResourceReady(isA(byte[].class), isA(Transition.class)); }
@Test public void testReceivesGifBytes() { String fakeUri = "content://fake"; InputStream testGifData = openGif(); mockUri(Uri.parse(fakeUri), testGifData); requestManager.as(byte[].class).apply(decodeTypeOf(GifDrawable.class)).load(fakeUri) .into(target); verify(target).onResourceReady(isA(byte[].class), isA(Transition.class)); }
@Test public void testReceivesBitmapBytes() { String fakeUri = "content://fake"; mockUri(fakeUri); requestManager.as(byte[].class).apply(decodeTypeOf(Bitmap.class)).load(fakeUri).into(target); verify(target).onResourceReady(isA(byte[].class), isA(Transition.class)); }
/** * Attempts to always load the resource using any registered {@link * com.bumptech.glide.load.ResourceDecoder}s that can decode any subclass of {@link Drawable}. * * <p> By default, may return either a {@link android.graphics.drawable.BitmapDrawable}, * but if additional decoders are registered for other {@link Drawable} subclasses, * any of those subclasses may also be returned. </p> * * @return A new request builder for loading a {@link Drawable}. */ public RequestBuilder<Drawable> asDrawable() { return as(Drawable.class); }
/** * Attempts to always load the resource as a {@link android.graphics.Bitmap}, even if it could * actually be animated. * * @return A new request builder for loading a {@link android.graphics.Bitmap} */ public RequestBuilder<Bitmap> asBitmap() { return as(Bitmap.class).apply(DECODE_TYPE_BITMAP); }
/** * Attempts always load the resource into the cache and return the {@link File} containing the * cached source data. * * <p>This method is designed to work for remote data that is or will be cached using {@link * com.bumptech.glide.load.engine.DiskCacheStrategy#DATA}. As a result, specifying a * {@link com.bumptech.glide.load.engine.DiskCacheStrategy} on this request is generally not * recommended. * * @return A new request builder for downloading content to cache and returning the cache File. */ public RequestBuilder<File> downloadOnly() { return as(File.class).apply(DOWNLOAD_ONLY_OPTIONS); }
/** * Attempts to always load a {@link File} containing the resource, either using a file path * obtained from the media store (for local images/videos), or using Glide's disk cache * (for remote images/videos). * * <p>For remote content, prefer {@link #downloadOnly()}. * * @return A new request builder for obtaining File paths to content. */ public RequestBuilder<File> asFile() { return as(File.class).apply(skipMemoryCacheOf(true)); }
RequestBuilder<Drawable> requestBuilder = Glide.with(mContext).as(Drawable.class);