Skip to content

Commit

Permalink
bumptech/glide#556 Data Uri / Base64 from JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
TWiStErRob committed May 14, 2016
1 parent 92b40e9 commit e10619e
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 44 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bumptech.glide.supportapp.github._556_data_uri;
package com.bumptech.glide.supportapp.github._556_data_uri_firebase;

import java.io.InputStream;

Expand All @@ -9,7 +9,7 @@
import com.firebase.client.Firebase;

// TODO https://github.com/bumptech/glide/wiki/Configuration#creating-a-glidemodule
class FirebaseGlideModule implements GlideModule {
public class FirebaseGlideModule implements GlideModule {
@Override public void applyOptions(Context context, GlideBuilder builder) {

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bumptech.glide.supportapp.github._556_data_uri;
package com.bumptech.glide.supportapp.github._556_data_uri_firebase;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bumptech.glide.supportapp.github._556_data_uri;
package com.bumptech.glide.supportapp.github._556_data_uri_firebase;

import java.io.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.bumptech.glide.supportapp.github._556_data_uri_firebase;

import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.supportapp.*;

public class TestFragment extends GlideImageFragment {
@Override protected void load(Context context) {
//Glide.setup(new GlideBuilder(context).setBitmapPool(new BitmapPoolAdapter()));
//Glide.get(context).register(FirebaseImage.class, InputStream.class, new FirebaseModelLoader.Factory(null));
Glide
.with(context)
.load(new FirebaseImage("data:image/jpeg;base64," + getString(R.string.glide_base64)))
.placeholder(R.drawable.glide_placeholder)
.error(R.drawable.glide_error)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(imageView);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.bumptech.glide.supportapp.github._556_data_uri_from_string;

import java.io.*;

import android.util.Base64;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;

class Base64StreamDataFetcher implements DataFetcher<InputStream> {
private final String base64;
public Base64StreamDataFetcher(String base64) {
this.base64 = base64;
}
@Override public InputStream loadData(Priority priority) throws Exception {
// depending on how you encoded it, the below is just a guess:
// here the full base64 is decoded into bytes and the bytes will be parsed by Glide
// TODO match the flags based on your possible inputs: e.g. add | Base64.URL_SAFE
byte[] raw = Base64.decode(base64, Base64.NO_WRAP);
return new ByteArrayInputStream(raw);
// ---- alternative
// if you don't want to delay decoding you can use something like:
// Base64InputStream (http://stackoverflow.com/a/19981216/253468)
// here the base64 bytes are passed to Base64InputStream and that to Glide
// so base64 decoding will be done later when Glide reads from the stream.
//return new Base64InputStream(new ByteArrayInputStream(base64.getBytes("utf-8")), Base64.NO_WRAP | Base64.URL_SAFE);
}
@Override public String getId() {
// not well-cacheable, I suggest to use .diskCacheStrategy(NONE)
return base64;
}
@Override public void cancel() {
}
@Override public void cleanup() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.bumptech.glide.supportapp.github._556_data_uri_from_string;

import java.io.InputStream;

import android.content.Context;

import com.bumptech.glide.*;
import com.bumptech.glide.module.GlideModule;

// TODO https://github.com/bumptech/glide/wiki/Configuration#creating-a-glidemodule
public class DataUriGlideModule implements GlideModule {
@Override public void applyOptions(Context context, GlideBuilder builder) {

}
@Override public void registerComponents(Context context, Glide glide) {
glide.register(String.class, InputStream.class, new DataUriModelLoader.Factory());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.bumptech.glide.supportapp.github._556_data_uri_from_string;

import java.io.InputStream;

import android.content.Context;

import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.*;
import com.bumptech.glide.load.model.stream.*;

// TODO read https://github.com/bumptech/glide/wiki/Downloading-custom-sizes-with-Glide
class DataUriModelLoader implements StreamModelLoader<String> {
private final ModelLoader<String, InputStream> defaultLoader;
public DataUriModelLoader(ModelLoader<String, InputStream> defaultLoader) {
this.defaultLoader = defaultLoader;
}

private static final String PREFIX = "data:image/png;base64,";
@Override public DataFetcher<InputStream> getResourceFetcher(final String model, int width, int height) {
if (model.startsWith(PREFIX)) { // TODO better matching based on your needs
return new Base64StreamDataFetcher(model.substring(PREFIX.length()));
} else {
return defaultLoader.getResourceFetcher(model, width, height);
}
}

public static class Factory implements ModelLoaderFactory<String, InputStream> {
public Factory() {
}
@Override public ModelLoader<String, InputStream> build(Context context, GenericLoaderFactory factories) {
// StreamStringLoader is hardcoded, because we've already overwritten String -> InputStream loader
// This will be better handled in Glide v4.
return new DataUriModelLoader(new StreamStringLoader(context));
}
@Override public void teardown() {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.bumptech.glide.supportapp.github._556_data_uri_from_string;

import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.supportapp.*;
import com.bumptech.glide.supportapp.utils.LoggingListener;

public class TestFragment extends GlideImageFragment {
@Override protected void load(final Context context) {
Glide
.with(context)
.load("data:image/png;base64," + getString(R.string.glide_base64))
.placeholder(R.drawable.glide_placeholder)
.error(R.drawable.glide_error)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(false)
.listener(new LoggingListener<String, GlideDrawable>())
.into(imageView);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.bumptech.glide.supportapp.github._556_data_uri_via_POST;

import java.io.IOException;

import org.json.*;

public class Image {
private final String key;
public Image(String key) {
this.key = key;
}

public String getKey() {
return key;
}

public String getUri() throws IOException {
return "http://httpbin.org/post";
}

public JSONObject getPayload() throws IOException {
try {
JSONObject object = new JSONObject();
object.put("imageKey", key);
return object;
} catch (JSONException e) {
throw new IOException("Invalid implementation", e);
}
}

@Override public String toString() {
return "Image for key=" + key;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.bumptech.glide.supportapp.github._556_data_uri_via_POST;

import java.io.InputStream;

import android.content.Context;

import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.*;
import com.bumptech.glide.load.model.stream.StreamModelLoader;

// TODO read https://github.com/bumptech/glide/wiki/Downloading-custom-sizes-with-Glide
class ImageModelLoader implements StreamModelLoader<Image> {
@Override public DataFetcher<InputStream> getResourceFetcher(final Image model, int width, int height) {
return new JSONImageFetcher(model);
}

public static class Factory implements ModelLoaderFactory<Image, InputStream> {
public Factory() {
}
@Override public ModelLoader<Image, InputStream> build(Context context, GenericLoaderFactory factories) {
return new ImageModelLoader();
}
@Override public void teardown() {
}
}
}
Loading

1 comment on commit e10619e

@myhcqgithub
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your

Please sign in to comment.