how can clear only cache of application not other data (sharedpreference) - android

I am working on app and saved data in sharedpreference and cache, I want clear only cache of application not sharedpreference data. How can I do it? It's my code
public void clearApplicationData(Context context)
{
File cache = context.getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(new File(appDir, s));Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
} public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
System.out.println("Directory not null");
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}

when i run application it's clear all the data of local memory
That is because of this line:
File appDir = new File(cache.getParent());
Get rid of it. Get rid of every reference to appDir.
If you want to delete getCacheDir(), then delete getCacheDir(), not other things, like the getParent() of getCacheDir().

Delete the Cache Directory
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
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();
}
you need permissions
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>

Related

Android clearing app cache clears provider also

I used following code to delete my app cache.
public void clearApplicationData() {
File cacheDirectory = getCacheDir();
File applicationDirectory = new File(cacheDirectory.getParent());
if (applicationDirectory.exists()) {
String[] fileNames = applicationDirectory.list();
for (String fileName : fileNames) {
if (!fileName.equals("lib")) {
deleteFile(new File(applicationDirectory, fileName));
}
}
}
}
public static boolean deleteFile(File file) {
boolean deletedAll = true;
if (file != null) {
if (file.isDirectory()) {
String[] children = file.list();
for (int i = 0; i < children.length; i++) {
deletedAll = deleteFile(new File(file, children[i])) && deletedAll;
}
} else {
deletedAll = file.delete();
}
}
return deletedAll;
}
Once I delete the code means it deletes the provider which I declared in manifest. Is there any way to clear cache without deleting content provider?
You can avoid this by not deleting database folder
if (!fileName.equals("lib")&&!fileName.equals("files")&&!fileName.equals("database")) {
deleteFile(new File(applicationDirectory, fileName));
}

Deleting older images from external storage after a limit

I have implemented offline caching in my app and for that I am storing images in external storage.I want that once my cached images folder limit reaches 30 then it starts replacing the older images with new ones.For that i implemented following deletion algorithm-
public static boolean deleteDir(File dir)
{
if (dir != null && dir.isDirectory())
{
String[] children = dir.list();
if(children.length>30)
{
int exceed=children.length-30;
for (int i = 0; i <exceed; i++)
{
boolean success = deleteDir(new File(dir, children[i]));
if (!success)
{
return false;
}
else
{
Log.e("deleted","file deleted");
}
}
}
}
return dir.delete();
}
But the above algorithm doesn't work as expected.It might deletes the newly added images.I also tried implementing below algorithm.But it also not working as expected.I failed to understand where I am going wrong.
public static boolean deleteDir(File dir)
{
if (dir != null && dir.isDirectory())
{
String[] children = dir.list();
if(children.length>30)
{
int exceed=children.length-30;
int destroy=(children.length-exceed)-1;
for (int i = children.length; i >destroy; i--)
{
boolean success = deleteDir(new File(dir, children[i]));
if (!success)
{
return false;
}
else
{
Log.e("deleted","file deleted");
}
}
}
}
return dir.delete();
}
Try this
public static void deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
File[] files = dir.listFiles();
if (files.length > 30) {
Arrays.sort(files, new Comparator<File>() {
#Override
public int compare(File o1, File o2) {
if (o1.lastModified() > o2.lastModified()) {
return 1;
} else if (o1.lastModified() < o2.lastModified()) {
return -1;
}
return 0;
}
});
for (int i = 0; i < files.length - 30; i++) {
files[i].delete();
}
}
}
}

Skip directory which exists in delete loop

Found a nice code to delete a directory including subdirs but is it possible to implement that a (sub)directory which for example exists (.data) is not being deleted?
Thanks for the help.
public static boolean deleteDir(File dir) {
if (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();
}
try this
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
if(children[i].equals("data")||children[i].equals(".data"))
continue;
else
if (!deleteDir(new File(dir, children[i])))
return false;
}
}
return dir.delete();
}
Maybe you can try this:
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
if(children.equals("data")){
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
}
return dir.delete();
}

How to clear cache of all running applications android?

In My application i tried to delete other applications cache memory using the following code. But it is deleting my application cache only Not the others applications.
code like this:
public void clearApplicationData()
{
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(new File(appDir, s));
Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
}
}
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();
}

Cache and temp files/folders remove android

Hi friend thanks for previous replies,
i am facing problem in removing cache and temp files/folders,
what i require is to clean the whole device temp files and cache from one app which is mine app
but here i am able to clean only my apps cache , here is my code
private void mAppMethod(List<App> mApps) {
// TODO Auto-generated method stub
// File f = g
for (int i = 0; i < mApps.size(); i++) {
File dir = new File("/data/data/"+mApps.get(i).getPackageName().concat("/cache"));
Log.e("dir "+dir, "is directory "+dir.isDirectory());
int j = clearCacheFolder(dir, 10);
if (dir!= null && dir.isDirectory())
Log.e("j", "rff "+dir.delete());
System.out.println(j+" rff "+dir.delete());
}
and my clear cache method as under
static int clearCacheFolder(final File dir, final int numDays) {
int deletedFiles = 0;
if (dir!= null && dir.isDirectory()) {
// System.out.println("here"+dir.delete());
Log.e("here", "here "+dir.isDirectory());
try {
Log.e("here1", "here1"+dir.listFiles());
for (File child:dir.listFiles()) {
Log.e("here11", "here11");
//first delete subdirectories recursively
if (child.isDirectory()) {
Log.e("here111", "here111");
deletedFiles += clearCacheFolder(child, numDays);
Log.e("here1111", "here1111");
}
Log.e("here11111", "here11111");
//then delete the files and subdirectories in this dir
//only empty directories can be deleted, so subdirs have been done first
if (child.lastModified() < new Date().getTime() - numDays * DateUtils.DAY_IN_MILLIS) {
Log.e("here111111", "here111111");
if (child.delete()) {
Log.e("here1111111", "here1111111");
deletedFiles++;
Log.e("here11111111", "here11111111");
}
}
}
}
catch(Exception e) {
Log.e("TAG", String.format("Failed to clean the cache, error %s", e.getMessage()));
}
}
return deletedFiles;
}
please help how can i clear the whole device cache,here i am getting every apps cache location i.e. dir to cache of all apps in device but when i want to delete them it returns false
please help any help is appreciable
i am able to clear cache of one app which is the one i am running this code but not for other apps
thanks in advance
Try with this code
public void clearApplicationData() {
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if(appDir.exists()){
String[] children = appDir.list();
for(String s : children){
if(!s.equals("lib")){
deleteDir(new File(appDir, s));
Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s +" DELETED *******************");
}
}
}
}
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();
}
This will delete your all data,and cache files in the application.

Categories

Resources