Skip to content

Commit

Permalink
Support transcoding Drawables to byte[]s.
Browse files Browse the repository at this point in the history
We do so by casting which isn’t great, but it makes the API work a bit
more as expected and it’s not super expensive in comparison to the 
compression call.
  • Loading branch information
sjudd committed Dec 27, 2017
1 parent d522ac7 commit 015d265
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -68,8 +67,6 @@ public void loadBitmap_asBytes_providesBytesOfBitmap() {
assertThat(BitmapFactory.decodeByteArray(data, 0, data.length)).isNotNull();
}

// TODO: Should this pass?
@Ignore
@Test
public void loadBitmapDrawable_asBytes_providesBytesOfBitmap() {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical);
Expand Down
13 changes: 11 additions & 2 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import com.bumptech.glide.load.resource.gif.StreamGifDecoder;
import com.bumptech.glide.load.resource.transcode.BitmapBytesTranscoder;
import com.bumptech.glide.load.resource.transcode.BitmapDrawableTranscoder;
import com.bumptech.glide.load.resource.transcode.DrawableBytesTranscoder;
import com.bumptech.glide.load.resource.transcode.GifDrawableBytesTranscoder;
import com.bumptech.glide.manager.ConnectivityMonitorFactory;
import com.bumptech.glide.manager.RequestManagerRetriever;
Expand Down Expand Up @@ -352,6 +353,9 @@ private static void throwIncorrectGlideModule(Exception e) {
new ResourceLoader.AssetFileDescriptorFactory(resources);
BitmapEncoder bitmapEncoder = new BitmapEncoder();

BitmapBytesTranscoder bitmapBytesTranscoder = new BitmapBytesTranscoder();
GifDrawableBytesTranscoder gifDrawableBytesTranscoder = new GifDrawableBytesTranscoder();

ContentResolver contentResolver = context.getContentResolver();

registry
Expand Down Expand Up @@ -481,8 +485,13 @@ Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitm
Bitmap.class,
BitmapDrawable.class,
new BitmapDrawableTranscoder(resources))
.register(Bitmap.class, byte[].class, new BitmapBytesTranscoder())
.register(GifDrawable.class, byte[].class, new GifDrawableBytesTranscoder());
.register(Bitmap.class, byte[].class, bitmapBytesTranscoder)
.register(
Drawable.class,
byte[].class,
new DrawableBytesTranscoder(
bitmapPool, bitmapBytesTranscoder, gifDrawableBytesTranscoder))
.register(GifDrawable.class, byte[].class, gifDrawableBytesTranscoder);

ImageViewTargetFactory imageViewTargetFactory = new ImageViewTargetFactory();
glideContext =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.bumptech.glide.load.resource.transcode;


import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import com.bumptech.glide.load.resource.gif.GifDrawable;

/**
* Obtains {@code byte[]} from {@link BitmapDrawable}s by delegating to a
* {@link ResourceTranscoder} for {@link Bitmap}s to {@code byte[]}s.
*/
public final class DrawableBytesTranscoder
implements ResourceTranscoder<Drawable, byte[]> {

private final BitmapPool bitmapPool;
private final ResourceTranscoder<Bitmap, byte[]> bitmapBytesTranscoder;
private final ResourceTranscoder<GifDrawable, byte[]> gifDrawableBytesTranscoder;

public DrawableBytesTranscoder(
BitmapPool bitmapPool,
ResourceTranscoder<Bitmap, byte[]> bitmapBytesTranscoder,
ResourceTranscoder<GifDrawable, byte[]> gifDrawableBytesTranscoder) {
this.bitmapPool = bitmapPool;
this.bitmapBytesTranscoder = bitmapBytesTranscoder;
this.gifDrawableBytesTranscoder = gifDrawableBytesTranscoder;
}

@Nullable
@Override
public Resource<byte[]> transcode(Resource<Drawable> toTranscode, Options options) {
Drawable drawable = toTranscode.get();
if (drawable instanceof BitmapDrawable) {
return bitmapBytesTranscoder.transcode(
BitmapResource.obtain(((BitmapDrawable) drawable).getBitmap(), bitmapPool), options);
} else if (drawable instanceof GifDrawable) {
return gifDrawableBytesTranscoder.transcode(toGifDrawableResource(toTranscode), options);
}
return null;
}

@SuppressWarnings("unchecked")
private static Resource<GifDrawable> toGifDrawableResource(Resource<Drawable> resource) {
return (Resource<GifDrawable>) (Resource<?>) resource;
}
}

0 comments on commit 015d265

Please sign in to comment.