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();
}
Related
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())
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
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));
I am beginner to Android development. There are several screens in my app and I can navigate back and forth between these screen using "backstack" feature. As I traverse through the app in depth, the memory usage of app is increasing. Sometimes it reaches up to 100 MB..!! It causes app to restart.
As I understand, below may be, possible reasons behind memory overuse:
1) App makes heavy use of images.
2) Backstacking is implemented. (There is only once activity used and other are fragments)
3) There is one background service running continuously for that app.
I am not sure whether these are the actual reasons. Please help me to find out the reasons. Any tips regarding memory overuse problem? One more question, What should be the ideal memory usage of Android app?
call these two method in your app it will delete all cache of images from android data and ur app will not crashed from memory issue
//this is for clear cache folder in android -> data folder which generate by gallry lazy loading
public static void trimCache(Context context) {
try {
// File dir = context.getCacheDir();
File dir = context.getExternalCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
System.out.println("delete cache folderrrrrrrrrr");
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void trimCacheinternal(Context context) {
try {
File dir = context.getCacheDir();
// File dir = context.getExternalCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
System.out.println("delete cache folderrrrrrrrrr");
}
} catch (Exception e) {
// TODO: handle exception
}
}
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);