Skip to content

Commit 5625007

Browse files
committed
Ext: Base64ImageDownloader
1 parent d8f0a58 commit 5625007

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

sample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ if (propFile.exists()) {
4040
}
4141
} else {
4242
android.buildTypes.release.signingConfig = null
43-
}
43+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*******************************************************************************
2+
* Copyright 2015 Sergey Tarasevich
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*******************************************************************************/
16+
package com.nostra13.universalimageloader.sample.ext;
17+
18+
import android.annotation.TargetApi;
19+
import android.content.Context;
20+
import android.os.Build;
21+
import android.util.Base64;
22+
23+
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
24+
25+
import java.io.ByteArrayInputStream;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
29+
/**
30+
* Downloader supporting "base64://..." URIs.
31+
* E.g.: "base64://data:image/jpeg;base64,/9j/4AAQSkZ..."
32+
*
33+
* @author mrleolink, Sergey Tarasevich (nostra13[at]gmail[dot]com)
34+
*/
35+
@TargetApi(Build.VERSION_CODES.FROYO)
36+
public class Base64ImageDownloader extends BaseImageDownloader {
37+
38+
public static final String BASE64_SCHEME = "base64";
39+
public static final String BASE64_URI_PREFIX = BASE64_SCHEME + "://";
40+
41+
public static final String BASE64_DATA_PREFIX = "base64,";
42+
43+
public Base64ImageDownloader(Context context) {
44+
super(context);
45+
}
46+
47+
public Base64ImageDownloader(Context context, int connectTimeout, int readTimeout) {
48+
super(context, connectTimeout, readTimeout);
49+
}
50+
51+
@Override
52+
public InputStream getStreamFromOtherSource(String imageUri, Object extra) throws IOException {
53+
if (imageUri.startsWith(BASE64_URI_PREFIX)) {
54+
return getStreamFormBase64(imageUri, extra);
55+
}
56+
return super.getStreamFromOtherSource(imageUri, extra);
57+
}
58+
59+
protected InputStream getStreamFormBase64(String imageUri, Object extra) {
60+
int dataStartIndex = imageUri.indexOf(BASE64_DATA_PREFIX) + BASE64_DATA_PREFIX.length();
61+
String base64 = imageUri.substring(dataStartIndex);
62+
return new ByteArrayInputStream(Base64.decode(base64, Base64.DEFAULT));
63+
}
64+
}

0 commit comments

Comments
 (0)