I have referred this link for creating CachedDataSource
for Exo-Player and successfully created it, but I am not sure, how to delete the cache of the loaded video?
There is a code example here , but I am not sure how to implement it.Any help will be appreciated.
Asked
Active
Viewed 4,331 times
3

Shashank Mishra
- 397
- 6
- 13
3 Answers
4
if you created a cache folder like it described in https://exoplayer.dev/downloading-media.html example for kotlin
@Synchronized
fun getDownloadCache(): Cache {
if (downloadCache == null) {
val downloadContentDirectory = File(getDownloadDirectory(), DOWNLOAD_CONTENT_DIRECTORY)
downloadCache = SimpleCache(downloadContentDirectory, NoOpCacheEvictor(), getDatabaseProvider())
}
return downloadCache as Cache
}
you can simply access this cache anywhere in your code to do this
fun clearCache() {
application.getDownloadCache().release()
}
Or clean single key with this:
fun removeKeyFromCache(key: String) {
CacheUtil.remove(application.getDownloadCache(), key)
}
application is an instance of your app, which can be created like this
lateinit var singleton: MyApp
open class MyApp: Application() {
override fun onCreate() {
super.onCreate()
singleton = this
}
}
And access it from anywhere:
private val application: MyApp = singleton

javier Cuervas
- 823
- 10
- 11
-
1That's really helped me thanks but, I got confused with what the is `key` would be, but I found static fun will generate the key in `CacheUtil` : `CacheUtil.generateKey(Uri.parse("www.test.com/1.mp3"))` – Abed Apr 18 '20 at 22:51
0
it's working for me:
public static void clearVideoCache(Context context){
try {
File dir = new File(context.getCacheDir(), cacheFolder);
deleteDir(dir);
} catch (Exception e) { e.printStackTrace();}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
return dir.delete();
} else if(dir!= null && dir.isFile()) {
return dir.delete();
} else {
return false;
}
}
full code:
public class VideoCache {
private static final String cacheFolder = "exoCache";
private static SimpleCache sDownloadCache;
public static SimpleCache getInstance(Context context) {
if (sDownloadCache == null) sDownloadCache = new SimpleCache(new File(context.getCacheDir(), cacheFolder), new LeastRecentlyUsedCacheEvictor(2000 * 1024 * 1024), new ExoDatabaseProvider(context));
return sDownloadCache;
}
public static void clearVideoCache(Context context){
try {
File dir = new File(context.getCacheDir(), cacheFolder);
deleteDir(dir);
} catch (Exception e) { e.printStackTrace();}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
return dir.delete();
} else if(dir!= null && dir.isFile()) {
return dir.delete();
} else {
return false;
}
}
}

Mkurbanov
- 197
- 3
- 13
-1
You can do this by accessing the directory where you save the cache, that way Do it in the OnDestroy method. I hope I help you
JAVA
File file = new File(mContext.cacheDir, "NameFolder")
file.delete()
KOTLIN
val file = File(mContext.getCacheDir(), "NameFolder")
file.delete()

ErickAlvz
- 21
- 2