Created
February 5, 2018 21:21
-
-
Save DevPicon/f8ffcc00198b3101013b435817cb8da0 to your computer and use it in GitHub Desktop.
Fresco Caching configuration, caching images and load images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Supplier<File> diskSupplier = new Supplier<File>() { | |
public File get() { | |
return getApplicationContext().getCacheDir(); | |
} | |
}; | |
DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder(this) | |
.setBaseDirectoryName("images") | |
.setBaseDirectoryPathSupplier(diskSupplier) | |
.setMaxCacheSize(100 * ByteConstants.MB) | |
.build(); | |
DiskCacheConfig diskSmallCacheConfig = DiskCacheConfig.newBuilder(this) | |
.setBaseDirectoryName("small_images") | |
.setBaseDirectoryPathSupplier(diskSupplier) | |
.build(); | |
ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this) | |
.setMainDiskCacheConfig(diskCacheConfig) | |
.setSmallImageDiskCacheConfig(diskSmallCacheConfig) | |
.build(); | |
Fresco.initialize(this, config); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cornershopapp.shopper.android.model; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.support.annotation.WorkerThread; | |
import android.util.Log; | |
import com.facebook.binaryresource.FileBinaryResource; | |
import com.facebook.cache.common.CacheKey; | |
import com.facebook.cache.common.WriterCallback; | |
import com.facebook.cache.disk.FileCache; | |
import com.facebook.common.memory.PooledByteBuffer; | |
import com.facebook.common.memory.PooledByteBufferInputStream; | |
import com.facebook.common.references.CloseableReference; | |
import com.facebook.datasource.BaseDataSubscriber; | |
import com.facebook.datasource.DataSource; | |
import com.facebook.imageformat.ImageFormat; | |
import com.facebook.imageformat.ImageFormatChecker; | |
import com.facebook.imagepipeline.cache.BufferedDiskCache; | |
import com.facebook.imagepipeline.cache.DefaultCacheKeyFactory; | |
import com.facebook.imagepipeline.core.ImagePipeline; | |
import com.facebook.imagepipeline.core.ImagePipelineFactory; | |
import com.facebook.imagepipeline.request.ImageRequest; | |
import com.microblink.metadata.ImageMetadata; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
/** | |
* Created by devpicon on 2/5/18. | |
*/ | |
public abstract class ImageDownloadSubscriber extends BaseDataSubscriber<CloseableReference<PooledByteBuffer>> { | |
private static final String TAG = "ImageDownloadSubscriber"; | |
private volatile boolean finished; | |
private ImageRequest imageRequest; | |
private File tempFile; | |
public ImageDownloadSubscriber(ImageRequest imageRequest) { | |
this.imageRequest = imageRequest; | |
} | |
@Override | |
public void onProgressUpdate(DataSource<CloseableReference<PooledByteBuffer>> dataSource) { | |
if (!finished) { | |
Log.d(TAG, "onProgressUpdate: Progress:" + ((int) (dataSource.getProgress() * 100))); | |
} | |
} | |
@Override | |
protected void onNewResultImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) { | |
Log.d(TAG, "onNewResultImpl() called with: dataSource = [" + dataSource + "]"); | |
if (!dataSource.isFinished()) { | |
Log.e(TAG, "onNewResultImpl: Not finished"); | |
return; | |
} | |
CloseableReference<PooledByteBuffer> pooledByteBuffer = dataSource.getResult(); | |
if (pooledByteBuffer == null) { | |
Log.e(TAG, "onNewResultImpl: PooledByteBuffer null"); | |
} | |
final PooledByteBufferInputStream inputStream; | |
if (pooledByteBuffer != null) { | |
inputStream = new PooledByteBufferInputStream(pooledByteBuffer.get()); | |
try { | |
ImageFormat imageFormat = ImageFormatChecker.getImageFormat(inputStream); | |
Log.i(TAG, "onNewResultImpl: The image format extension is " + imageFormat.getFileExtension()); | |
onSuccess(); | |
} catch (IOException e) { | |
onFail(new Throwable(e)); | |
} finally { | |
try { | |
inputStream.close(); | |
} catch (IOException e) { | |
onFail(new Throwable(e)); | |
} | |
CloseableReference.closeSafely(pooledByteBuffer); | |
} | |
} | |
} | |
@Override | |
protected void onFailureImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) { | |
Log.d(TAG, "onFailureImpl() called with: dataSource = [" + dataSource + "]"); | |
finished = true; | |
} | |
@WorkerThread | |
protected abstract void onProgress(int progress); | |
@WorkerThread | |
protected abstract void onSuccess(); | |
@WorkerThread | |
protected abstract void onFail(Throwable throwable); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Uri uri = Uri.parse(imageModel.getUrl()); | |
DraweeController controller = Fresco.newDraweeControllerBuilder() | |
.setUri(uri) | |
.setOldController(simpleDraweeViewOrderImage.getController()) | |
.build(); | |
GenericDraweeHierarchy hierarchy = GenericDraweeHierarchyBuilder.newInstance(itemView.getResources()) | |
.setPlaceholderImage(R.drawable.image_placeholder) | |
.setFailureImage(R.drawable.image_broken_placeholder) | |
.setProgressBarImage(R.drawable.indeterminate_progress) | |
.build(); | |
simpleDraweeViewOrderImage.setHierarchy(hierarchy); | |
simpleDraweeViewOrderImage.setController(controller); | |
} else { | |
simpleDraweeViewOrderImage.setImageURI(Uri.parse(new File(imageModel.getLocalPath()).toString())); | |
} | |
if (images != null && images.length > 0) { | |
simpleDraweeViewOrderImage.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
showPicker(position, images); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final ImagePipeline imagePipeline = Fresco.getImagePipeline(); | |
imagePipeline.clearCaches(); | |
String url = modifiedModel.getUrl(); | |
final Uri parsedImageUrl = Uri.parse(url); | |
ImageRequest imageRequest = getImageRequest(parsedImageUrl); | |
imagePipeline.fetchEncodedImage(imageRequest, new OnRequestImageModelListener()); | |
/*ImageDownloadSubscriber subscriber = new ImageDownloadSubscriber(imageRequest) { | |
@Override | |
protected void onProgress(int progress) { | |
} | |
@Override | |
protected void onSuccess() { | |
boolean inBitmapMemoryCache = imagePipeline.isInBitmapMemoryCache(parsedImageUrl); | |
Log.d(TAG, "onSuccess: onSuccess is in memory cache? " + inBitmapMemoryCache); | |
} | |
@Override | |
protected void onFail(Throwable throwable) { | |
} | |
}; | |
dataSource.subscribe(subscriber, new DefaultExecutorSupplier(Runtime.getRuntime().availableProcessors()).forBackgroundTasks());*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment