Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fdln/6757a2f8ef67ba1ff426 to your computer and use it in GitHub Desktop.
Save fdln/6757a2f8ef67ba1ff426 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment