Fresco cache,

Source: Internet
Author: User

Fresco cache,

Fresco Cache

Create a file ImagePipelineConfigFactory:
 
public class ImagePipelineConfigFactory { private static final String IMAGE_PIPELINE_CACHE_DIR = "imagepipeline_cache"; private static ImagePipelineConfig sImagePipelineConfig; private static final int MAX_HEAP_SIZE = (int) Runtime.getRuntime().maxMemory(); public static final int MAX_DISK_CACHE_SIZE = 300 * ByteConstants.MB; public static final int MAX_MEMORY_CACHE_SIZE = MAX_HEAP_SIZE / 3; /** * Creates config using android http stack as network backend. */ public static ImagePipelineConfig getImagePipelineConfig(Context context) { if (sImagePipelineConfig == null) { ImagePipelineConfig.Builder configBuilder = ImagePipelineConfig.newBuilder(context); configureCaches(configBuilder, context); sImagePipelineConfig = configBuilder.build(); } return sImagePipelineConfig; } /** * Configures disk and memory cache not to exceed common limits */ private static void configureCaches(ImagePipelineConfig.Builder configBuilder, Context context) { final MemoryCacheParams bitmapCacheParams = new MemoryCacheParams( MAX_MEMORY_CACHE_SIZE, // Max total size of elements in the cache Integer.MAX_VALUE, // Max entries in the cache MAX_MEMORY_CACHE_SIZE, // Max total size of elements in eviction queue Integer.MAX_VALUE, // Max length of eviction queue Integer.MAX_VALUE); // Max cache entry size configBuilder .setBitmapMemoryCacheParamsSupplier( new Supplier
 
  () { @Override public MemoryCacheParams get() { return bitmapCacheParams; } }) .setMainDiskCacheConfig(DiskCacheConfig.newBuilder() .setBaseDirectoryPath(getExternalCacheDir(context)) .setBaseDirectoryName(IMAGE_PIPELINE_CACHE_DIR) .setMaxCacheSize(MAX_DISK_CACHE_SIZE) .build()); } public static File getExternalCacheDir(final Context context) { if (hasExternalCacheDir()) return context.getExternalCacheDir(); // Before Froyo we need to construct the external cache dir ourselves final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/"; return createFile(Environment.getExternalStorageDirectory().getPath() + cacheDir, ""); } public static boolean hasExternalCacheDir() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO; } public static File createFile(String folderPath, String fileName) { File destDir = new File(folderPath); if (!destDir.exists()) { destDir.mkdirs(); } return new File(folderPath, fileName); } } 
 
 

MyApp code. Remember to configure it in the configuration file:

public class App extends Application {    @Override    public void onCreate() {        super.onCreate();        Fresco.initialize(this, ImagePipelineConfigFactory.getImagePipelineConfig(this));    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.