-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bumptech/glide#556 Data Uri / Base64 from JSON
- Loading branch information
1 parent
92b40e9
commit e10619e
Showing
15 changed files
with
344 additions
and
44 deletions.
There are no files selected for viewing
40 changes: 0 additions & 40 deletions
40
src/glide3/java/com/bumptech/glide/supportapp/github/_556_data_uri/TestFragment.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...p/github/_556_data_uri/FirebaseImage.java → ..._556_data_uri_firebase/FirebaseImage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ub/_556_data_uri/FirebaseModelLoader.java → ...ata_uri_firebase/FirebaseModelLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...glide3/java/com/bumptech/glide/supportapp/github/_556_data_uri_firebase/TestFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
36 changes: 36 additions & 0 deletions
36
...m/bumptech/glide/supportapp/github/_556_data_uri_from_string/Base64StreamDataFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...va/com/bumptech/glide/supportapp/github/_556_data_uri_from_string/DataUriGlideModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...va/com/bumptech/glide/supportapp/github/_556_data_uri_from_string/DataUriModelLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...de3/java/com/bumptech/glide/supportapp/github/_556_data_uri_from_string/TestFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
src/glide3/java/com/bumptech/glide/supportapp/github/_556_data_uri_via_POST/Image.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...e3/java/com/bumptech/glide/supportapp/github/_556_data_uri_via_POST/ImageModelLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} | ||
} |
Oops, something went wrong.
e10619e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your