Skip to content

Instantly share code, notes, and snippets.

@chethann
Created July 28, 2016 06:28
Show Gist options
  • Save chethann/ecf6717a44f0ff869fd74406e6209e3b to your computer and use it in GitHub Desktop.
Save chethann/ecf6717a44f0ff869fd74406e6209e3b to your computer and use it in GitHub Desktop.
Fresco (android image loading library) useful utility functions
package com.android.fresco;
import org.apache.commons.lang3.StringUtils;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import com.facebook.cache.common.CacheKey;
import com.facebook.cache.common.SimpleCacheKey;
import com.facebook.cache.common.WriterCallback;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.cache.DefaultCacheKeyFactory;
import com.facebook.imagepipeline.cache.StagingArea;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.core.ImagePipelineFactory;
import com.facebook.imagepipeline.image.EncodedImage;
import com.facebook.imagepipeline.request.ImageRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FrescoUtils {
public static boolean checkIfPresentInMemCache(String imageUrl) {
if(StringUtils.isEmpty(imageUrl)){
return false;
}
return Fresco.getImagePipeline().isInBitmapMemoryCache(Uri.parse(imageUrl));
}
public static boolean isPresentInDiskCache(String imageUrl) {
if(StringUtils.isEmpty(imageUrl)){
return false;
}
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(ImageRequest.fromUri(imageUrl));
StagingArea stagingArea = StagingArea.getInstance();
EncodedImage result = stagingArea.get(cacheKey);
if(result != null){
result.close();
return true;
}
return ImagePipelineFactory.getInstance().getMainDiskStorageCache().hasKey(cacheKey) || ImagePipelineFactory.getInstance().getSmallImageDiskStorageCache().hasKey(cacheKey);
}
//check if present in cache synchronously
public static boolean isPresentInCache(String imageUrl){
if(StringUtils.isEmpty(imageUrl)){
return false;
}
return checkIfPresentInMemCache(imageUrl) || isPresentInDiskCache(imageUrl);
}
//add manually to cache!
public static void addToDiskCache(String url, Bitmap bitmap){
try {
CacheKey cacheKey = new SimpleCacheKey(url);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
final byte[] byteArray = stream.toByteArray();
Fresco.getImagePipelineFactory().getMainDiskStorageCache().insert(cacheKey, new WriterCallback() {
@Override
public void write(OutputStream outputStream) throws IOException {
outputStream.write(byteArray);
}
});
} catch (IOException cacheWriteException) {
Log.e("Error", cacheWriteException.getMessage());
}
}
public static void clearMemCache() {
ImagePipeline imagePipeline = Fresco.getImagePipeline();
imagePipeline.clearMemoryCaches();
}
public static void clearDiskCache() {
Fresco.getImagePipeline().clearDiskCaches();
}
//remove from cache manually
public static void removeFromCache(String imageUrl) {
if(StringUtils.isEmpty(imageUrl)){
return;
}
if(Fresco.getImagePipeline() != null){
Fresco.getImagePipeline().evictFromCache(Uri.parse(imageUrl));
}else{
Fresco.getImagePipeline().evictFromCache(Uri.parse(imageUrl));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment