Search code examples
androidencryptionpicassoimage-caching

Encryption in disk caching Picasso android


I am planning on implementing, Picasso with OKHTTP for Disk caching of images. But I also plan on having encryption on images caches on disk. What should I do? I couldn't find a single helpful link, that can guide me in the right direction. Any kind of help would be greatly appreciated.


Solution

  • If somebody is looking for an answer, here is how I achieved it. Firstly I switched to UIL since it provides more customization than Picasso.

    Then I customized the built in diskCache mechanism.

     ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(getApplicationContext());
        builder.threadPriority(Thread.NORM_PRIORITY - 2);
        builder.threadPoolSize(5);
        builder.imageDownloader(new CustomImageDownaloder(this, new OkHttpClient()));
        builder.diskCache(new CustomDiskCache(cacheDir));
    

    Now in CustomDiskCache just override the methods where it read bytes from network and save them to disk, read bytes encrypt them and save them. Similarly, when you are about to read from cache file, decrypt bytes and convert them to bitmap.

    Hope it helps.