How to delete all files from Android Picasso cache

牧云@^-^@ 提交于 2021-01-07 10:02:16

问题


I am working on a project where iam using picasso (http://square.github.io/picasso/) to load all the images in a Recycler View, i wanted to remove all the files from the Picasso cache at the time of logging out of the application. Can you please help me how to do that, i dont want to invalidate every single file separately, also where does it save all the files?


回答1:


I myself was searching on how to clear the whole cache few days back and stumbled upon it on Stackoverflow itself, cannot seem to find the thread now but here's my code.

Add this class to the com.squareup.picasso package :

 package com.squareup.picasso;

    public class PicassoTools {

        public static void clearCache (Picasso picasso) {
            picasso.cache.clear();
        }
    }

And simply call this on logout method :

PicassoTools.clearCache(Picasso.with(context));



回答2:


you can mark the picasso load with no cache by the following:

Picasso.with(getContext()).load(data.get(pos).getFeed_thumb_image()).memoryPolicy(MemoryPolicy.NO_CACHE).into(image);



回答3:


The safest way to completely clear the Picasso image cache (disk and memory) is to build and use your own Picasso instance using the command:

Picasso myPicasso = Picasso.Builder(context).build();

To clear the cache, you just shutdown myPicasso instance and the rebuild it. Don't call Picasso.setSingletonInstance(myInstance) because Picasso does not allow shutting down the single instance and the shutdown call will throw an exception explaining this restriction.

The following method will clear the Picasso cache:

@NotNull Picasso clearCache(@NotNull Picasso picasso, @NotNull Context context) {
    picasso.shutdown();
    return Picasso.Builder(context).build();
}

When you want to clear the cache, you can just add the following line at the appropriate place in your code where you want to clear the cache:

myPicasso = clearCache(myPicasso);

I use Kotlin which makes it easy to write a shadow Picasso class so that the rest of your application has no idea that it's not using the default Picasso singleton. Here's how I do it:

class App : Application() {
    lateinit var picasso: Picasso

    companion object {
        @JvmStatic
        var instance: App by notNullSingleValue()
    }

    override fun onCreate() {
        super.onCreate()
        instance = this

        // Install custom Picasso instance to 
        // make it easy to clear it's cache.
        picasso = Picasso.Builder(instance).build()
    }
}

/**
 * Custom Picasso singleton so that the rest of the app
 * can use Picasso.with(context) as if using the normal
 * Picasso singleton.
 */
object Picasso {
    /**
     * Ingore passed context and just pass back
     * the custom singleton instance.
     */
    fun with(context: Context): Picasso {
        return App.instance.picasso
    }

    /**
     * Clears the Picasso cache by shutting it down
     * and then recreating the singleton instance.
     */ 
    fun clearCache() {
        App.instance.picasso.shutdown()
        App.instance.picasso = Picasso.Builder(App.instance).build()
    }
}

To clear the cache simply involves this call:

Picasso.clearCache()

And that does it! All you old Picasso calls can just remain the way they were before adding this custom instance. You will only need to replace your old

import com.squareup.picasso.Picasso

with

import <your-package-path>.app.Picasso

and your app will be able to use the Picasso.with(context) calls that probably already exist.

An alternative approach (possibly more elegant):

You might be to able build a custom instance using a custom OKHttp instance, and then you might be able to clear the OkHttp cache directly from the OkHttp instance, but I haven't tried this strategy because the Kotlin method I'm using suits my purpose.



来源:https://stackoverflow.com/questions/39333597/how-to-delete-all-files-from-android-picasso-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!