Delete cached files - WebView Android 4.4+ - android

I managed to delete cached files created by WebView using:
Clearing android cache,
Clear Application cache on exit in android
However for Android 4.4 that solution doesn't work properly, since the files are cached in:
/data/data/com.app.package/app_webview/
instead of:
/data/data/com.app.package/cache/
The above path can be obtained by the official command getCacheDir().
An approach could be hard-coding the path obtained through Get Application Directory
However, is there any [official]/proper solution to address this?

you can use this code
private static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (String aChildren : children) {
boolean success = deleteDir(new File(dir, aChildren));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir != null && dir.delete();
}
void trimCache() {
try {
String pathadmob = this.getFilesDir().getParent() + "/app_webview";
File dir = new File(pathadmob);
if (dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Also, here was generated all admob cache 4.4+, you can use a code to verify how many times the user use the app, and delete the admob cache when the user reached the limit.

Normally, to clear WebView cache, use this WebView API: WebView.clearCache(true);

Related

How to clear cache in android version 7.1

Please help with clearing cache in android. I am able to delete cache with below code on android version 6.0. When i am trying it in 7.1 nothing changes. what am i missing?
//method to delete cache- issue reported
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
Log.d("cache clear", "cache delete "+dir);
} catch (Exception e) { e.printStackTrace();}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
Log.d("cache clear", "cache delete 1");
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;
}
}
From Android API 19 you can do that:
((ActivityManager)context.getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();
Official docs:
clearApplicationUserData
added in API level 19
public boolean clearApplicationUserData ()
Permits an application to erase its own data from disk. This is equivalent to the user choosing to clear the app's data from within the device settings UI. It erases all dynamic data associated with the app -- its private data and data in its private area on external storage -- but does not remove the installed application itself, nor any OBB files. It also revokes all runtime permissions that the app has acquired, clears all notifications and removes all Uri grants related to this application.
Returns
boolean true if the application successfully requested that the application's data be erased; false otherwise.
EDIT
From your Activity don't call
deleteCache(this)
but call
deleteCache(getApplicationContext())

How much memory my app should preserve after closing it

Once I open my application it preserves about 7MB (Via Android Studio Memory Monitor), and during using it (Populating a listView with custom items) it pops to about 9MB : 12MB
After I close the application and use 'Initiate GC' from Android Studio it goes down to about 8.3MB.
So, does this mean / indicates that I have a memory leak?
Shouldn't it goes back to 7MB as it started?
or Shouldn't it be 0MB as my application closed?
If you are looking for delete cache of your own application then simply delete your cache directory and its all done !
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
} catch (Exception e) {}
}
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;
}
}
And you may require following permission to add in your manifest file in order to delete cache of other application
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
Reference : Clear Cache in Android Application programmatically

Android Picasso clear image from disk and cache

I'm using Picasso for image loading, I use it with NetworkPolicy.OFFLINE,
but at some point I want to update the image from internet and I'm trying with
Picasso.with(context).invalidate(url);
Picasso.with(context).load(url).memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).networkPolicy(NetworkPolicy.NO_CACHE);
But the image is still taken from disk, only the cache is invalidated.
Use this:
Picasso.with(context).load(url).memoryPolicy(MemoryPolicy.NO_CACHE).into(image);
You can clear cache programmatically using below code. You can use this code before you need to get images from API.
If you want PICASSO to never cache images you can use code shared by waseem. but if you need it to cache but only sometimes you need to get images from API you can clear cache.
public static void trimCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
// TODO: handle exception
}
}
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 {
return false;
}
So I was calling this when I did not have any views available yet.
The key is to call .into() method that triggers overwriting the cache.
So I use only this:
Picasso.with(context).load(url).into(new ImageView(context));

android kitkat webview cache issue

One day, My customer says "Webview cache seem to stack data area, not cache area. My device is Optimus G 4.4.2 Kitkat".
Whether cache is really accumulate in the data area, i checked my 4.4.2 device.
so, cache was being really accumulated the data area.
and, it does not delete the following way.
protected void trimCache(Context context) {
try {
dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
children = dir.list();
for (String aChildren : children) {
if (deleteDir(new File(dir, aChildren))) continue;
return false;
}
}
assert dir != null;
return dir.delete();
}
and, Webview.clearCache() and Context.deleteDatabase("") .
I have two Question.
How can i delete cache in the data area?
if i can't delete cache in the data area, How i can turn off android webview cache.
p.s. I'm not native speaker, so my english is not enough.
How can i delete cache in the data area?
You can take a look at this link for further info as to how to delete the cache data. There is a recursive method that, that guy is using.
if i can't delete cache in the data area, How i can turn off android webview cache.
You can try using
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setAppCacheEnabled(false);
But more importantly, just after creating your webview, before loading any pages, you can clear the cache, as mentioned in the documentation here.
mWebView.clearCache(true)
i am using this to clear cache from data.
try {
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("databases") &&!s.equals("lib") && !s.equals("shared_prefs")) {
deleteDir(new File(appDir, s));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

How to delete a folder when user selects uninstall my application in android

I would like to know about how to delete a folder once user select uninstall button for my application.
I want it by programmatically is there any chance to do it...
If so let me know possible solution for it.
thanks in advance.
If you created any folders on a device's external storage... there is no way for you to call code when the user uninstalls your app. Certain things are removed automatically (databases, anything written to Internal Storage), but not folders on external storage.
EDIT - As pointed out by Stephan, if you are targeting API Level 8 or higher, you can use Context.getExternalFilesDir() for your external files and those will be removed on uninstall.
Here is an idea. If you are concerned about the fact that the files that are not deleted during an uninstall will be messing up the user onboarding process when he later re-installs your app as in this question then you could simply ensure that all data is deleted at the moment your app is reinstalled (or simply installed for that matter). It's a "if Mohammed does not go to the mountain, the mountain will go to Mohammed" kind of hack. Obviously you would have to set a flag in shared preferences so the process of deleting the content of ExternalStorageDir is only performed once before the users very FIRST interaction with your app here is some sample code:
SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstInteraction = sharedPreferences.getBoolean("isFirstUsage", true);
if(isFirstInteraction){
trimCache(this);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putBoolean("isFirstUsage",false);
editor.apply();
}
//delete files from external files dir
public static void trimStorage(Context context) {
try {
File dir = context.getExternalFilesDir();
if (dir != null && dir.isDirectory()) {
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) {
Log.d("deletion","failed at "+children[i]);
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}

Categories

Resources