Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save heitara/322f4d5e9484ca1b9ec8 to your computer and use it in GitHub Desktop.
Save heitara/322f4d5e9484ca1b9ec8 to your computer and use it in GitHub Desktop.
public static Picasso getImageLoader(Context ctx) {
if(sPicassoInstance == null) {
Picasso.Builder builder = new Picasso.Builder(ctx);
sTransformRequest = new Picasso.RequestTransformer() {
@Override
public Request transformRequest(Request request) {
Uri uri = request.uri;
if(uri.getScheme().startsWith("http")) {
String urlString = uri.toString();
String cachedURL = getCachedURL(urlString);
if(urlString != cachedURL) {
request = request.buildUpon().setUri(Uri.parse(cachedURL)).build();
}
}
return request;
}
};
builder.requestTransformer(sTransformRequest);
sCache = new LruCache(ctx);
builder.memoryCache(sCache);
sPicassoInstance = builder.build();
// sPicassoInstance.setDebugging(true);
}
return sPicassoInstance;
}
public static Bitmap getCachedImage(String imageUrl) {
Bitmap bmp = null;
if(sPicassoInstance != null) {
if(sCache != null) {
Uri uri = null;
uri = Uri.parse(imageUrl);
Request.Builder data = new Request.Builder(uri);
Request finalData = sTransformRequest.transformRequest(data.build());
String key = createKey(finalData, new StringBuilder());
bmp = sCache.get(key);
}
}
return bmp;
}
//copy createKey method from picasso util class
static String createKey(Request data, StringBuilder builder) {
if (data.uri != null) {
String path = data.uri.toString();
builder.ensureCapacity(path.length() + 50);
builder.append(path);
} else {
builder.ensureCapacity(50);
builder.append(data.resourceId);
}
builder.append('\n');
if (data.rotationDegrees != 0) {
builder.append("rotation:").append(data.rotationDegrees);
if (data.hasRotationPivot) {
builder.append('@').append(data.rotationPivotX).append('x').append(data.rotationPivotY);
}
builder.append('\n');
}
if (data.targetWidth != 0) {
builder.append("resize:").append(data.targetWidth).append('x').append(data.targetHeight);
builder.append('\n');
}
if (data.centerCrop) {
builder.append("centerCrop\n");
} else if (data.centerInside) {
builder.append("centerInside\n");
}
if (data.transformations != null) {
//noinspection ForLoopReplaceableByForEach
for (int i = 0, count = data.transformations.size(); i < count; i++) {
builder.append(data.transformations.get(i).key());
builder.append('\n');
}
}
return builder.toString();
}
//How to get a string representation and add it to the webview
Bitmap bmp = getCachedImage("http://example.com/image.jpg");
String result = news.getImage();
if(bmp != null) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
result = "data:image/png;base64," + imgageBase64;
}
htmlContent = "<img src=\"" + result + "\" />" ;
//then simply load the htmlContent
@icyfox-bupt
Copy link

Will the Bitmap to Base64 waste too much time ?

@noundla
Copy link

noundla commented Jan 5, 2016

What is the getCachedURL() method implementation? Please share me that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment