Skip to content

Commit c28c4d9

Browse files
committed
Support Base64 images
1 parent 8d34490 commit c28c4d9

File tree

4 files changed

+787
-9
lines changed

4 files changed

+787
-9
lines changed

library/src/main/java/com/nostra13/universalimageloader/core/download/BaseImageDownloader.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
import android.provider.ContactsContract;
2727
import android.provider.MediaStore;
2828
import android.webkit.MimeTypeMap;
29+
2930
import com.nostra13.universalimageloader.core.DisplayImageOptions;
3031
import com.nostra13.universalimageloader.core.assist.ContentLengthInputStream;
32+
import com.nostra13.universalimageloader.utils.Base64;
3133
import com.nostra13.universalimageloader.utils.IoUtils;
3234

3335
import java.io.BufferedInputStream;
@@ -47,6 +49,7 @@
4749
* {@link URLConnection} is used to retrieve image stream from network.
4850
*
4951
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
52+
* @author Leo Link (mr[dot]leolink[at]gmail[dot]com)
5053
* @since 1.8.0
5154
*/
5255
public class BaseImageDownloader implements ImageDownloader {
@@ -94,6 +97,8 @@ public InputStream getStream(String imageUri, Object extra) throws IOException {
9497
return getStreamFromAssets(imageUri, extra);
9598
case DRAWABLE:
9699
return getStreamFromDrawable(imageUri, extra);
100+
case BASE64:
101+
return getStreamFromBase64(imageUri, extra);
97102
case UNKNOWN:
98103
default:
99104
return getStreamFromOtherSource(imageUri, extra);
@@ -263,6 +268,19 @@ protected InputStream getStreamFromDrawable(String imageUri, Object extra) {
263268
return context.getResources().openRawResource(drawableId);
264269
}
265270

271+
/**
272+
* Retrieves {@link InputStream} of a {@link android.util.Base64}-encoded string.
273+
*
274+
* @param imageUri {@link android.util.Base64}-encoded string
275+
* @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object)
276+
* DisplayImageOptions.extraForDownloader(Object)}; can be null
277+
* @return {@link InputStream} of image
278+
*/
279+
protected InputStream getStreamFromBase64(String imageUri, Object extra) {
280+
String base64 = imageUri.substring(imageUri.indexOf("base64,") + 7);
281+
return new ByteArrayInputStream(Base64.decode(base64, Base64.DEFAULT));
282+
}
283+
266284
/**
267285
* Retrieves {@link InputStream} of image by URI from other source with unsupported scheme. Should be overriden by
268286
* successors to implement image downloading from special sources.<br />

library/src/main/java/com/nostra13/universalimageloader/core/download/ImageDownloader.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Implementations have to be thread-safe.
2727
*
2828
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
29+
* @author Leo Link (mr[dot]leolink[at]gmail[dot]com)
2930
* @since 1.4.0
3031
*/
3132
public interface ImageDownloader {
@@ -42,17 +43,29 @@ public interface ImageDownloader {
4243
InputStream getStream(String imageUri, Object extra) throws IOException;
4344

4445
/** Represents supported schemes(protocols) of URI. Provides convenient methods for work with schemes and URIs. */
45-
public enum Scheme {
46-
HTTP("http"), HTTPS("https"), FILE("file"), CONTENT("content"), ASSETS("assets"), DRAWABLE("drawable"), UNKNOWN("");
46+
enum Scheme {
47+
HTTP("http", true),
48+
HTTPS("https", true),
49+
FILE("file", true),
50+
CONTENT("content", true),
51+
ASSETS("assets", true),
52+
DRAWABLE("drawable", true),
53+
BASE64("^data:image/[a-zA-Z]{3,};base64,([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)", false),
54+
UNKNOWN("", false);
4755

4856
private String scheme;
49-
private String uriPrefix;
57+
private String pattern;
5058

51-
Scheme(String scheme) {
52-
this.scheme = scheme;
53-
uriPrefix = scheme + "://";
59+
Scheme(String scheme, boolean isScheme) {
60+
this.scheme = scheme;
61+
if (isScheme) {
62+
this.pattern = "^" + scheme + "://.+?$";
63+
} else {
64+
this.pattern = scheme;
65+
}
5466
}
5567

68+
5669
/**
5770
* Defines scheme of incoming URI
5871
*
@@ -71,20 +84,24 @@ public static Scheme ofUri(String uri) {
7184
}
7285

7386
private boolean belongsTo(String uri) {
74-
return uri.toLowerCase(Locale.US).startsWith(uriPrefix);
87+
return uri.toLowerCase(Locale.US).matches(pattern);
7588
}
7689

7790
/** Appends scheme to incoming path */
7891
public String wrap(String path) {
79-
return uriPrefix + path;
92+
return getUriPrefix() + path;
8093
}
8194

8295
/** Removed scheme part ("scheme://") from incoming URI */
8396
public String crop(String uri) {
8497
if (!belongsTo(uri)) {
8598
throw new IllegalArgumentException(String.format("URI [%1$s] doesn't have expected scheme [%2$s]", uri, scheme));
8699
}
87-
return uri.substring(uriPrefix.length());
100+
return uri.substring(getUriPrefix().length());
88101
}
102+
103+
private String getUriPrefix() {
104+
return scheme + "://";
105+
}
89106
}
90107
}

0 commit comments

Comments
 (0)