I want to delete data on backpress of activity in android - android

on back press of activity delete data this code work but appication is crash
public void onBackPressed() {
File dir = context.getCacheDir();
if(dir!= null && dir.isDirectory()){
File[] children = dir.listFiles();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
File temp;
for (int i = 0; i < children.length; i++) {
temp = children[i];
temp.delete();
}
}}}
Thanks in advance.

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));
}

List all files with certain extension in all folders or sub folders of sdcard - android

This may seem like an old question but I have tried all solutions and this doesn't seem to work for me. I use the following code for listing all files with extension .docx in my ListView. However, this doesn't work properly for some reason and lists only all files under root.
In onCreate:
view = (LinearLayout) findViewById(R.id.view);
//getting SDcard root path
root = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath());
getfile(root);
for (int i = 0; i < fileList.size(); i++) {
TextView textView = new TextView(this);
if(!fileList.get(i).isDirectory() & fileList.get(i).getName().endsWith(".docx") || fileList.get(i).getName().endsWith(".doc")) {
textView.setText((i+1)+". "+fileList.get(i).getName());
Toast.makeText(this, fileList.get(j).getAbsolutePath(),Toast.LENGTH_LONG).show();
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
.....
}
});
view.addView(textView);
}
else {
getfile( new File(fileList.get(i).getAbsolutePath())); // its directory so similar code but new filelist instance
for (int k = 0; k < fileList.size(); k++) {
//TextView textView = new TextView(this);
if(!fileList.get(k).isDirectory() & fileList.get(k).getName().endsWith(".docx") || fileList.get(k).getName().endsWith(".doc")) {
textView.setText((k+1)+". "+fileList.get(i).getName());
Toast.makeText(this, fileList.get(j).getAbsolutePath(),Toast.LENGTH_LONG).show();
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
...
}
});
view.addView(textView);
}
else {
}
}
}
getFile ArrayList function:
public ArrayList<File> getfile(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
} else {
if (listFile[i].getName().endsWith(".docx")
|| listFile[i].getName().endsWith(".doc"))
{
fileList.add(listFile[i]);
}
}
}
}
return fileList;
}

get images' names in directory

How can I get only images' names in known directory.
Trying use this (from here)
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "yourpath");
for (File f : yourDir.listFiles()) {
if (f.isFile())
String name = f.getName();
// make something with the name
}
but don't works.
How can I do this?
private ArrayList<File> fileList = new ArrayList<File>();
//getting SDcard root path
File root = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath());
getfile(root);
for (int i = 0; i < fileList.size(); i++) {
// getting file name
System.out.println(fileList.get(i).getName());
if (fileList.get(i).isDirectory()) {
                teSystem.out.println("This is Directory not an image");
            }
}
// getfile(file) method
public ArrayList<File> getfile(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
fileList.add(listFile[i]);
getfile(listFile[i]);
} else {
if (listFile[i].getName().endsWith(".png")
|| listFile[i].getName().endsWith(".jpg")
|| listFile[i].getName().endsWith(".jpeg")
|| listFile[i].getName().endsWith(".gif"))
{
fileList.add(listFile[i]);
}
}
}
}
return fileList;
}
hope it will help your purpose.

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.

How to Search Images from SDCard?

I have done an Application which shows the images which are stored in drawable folder.
Now I want to implement an application which Searches images from SDcard/Phone Memory and display them in Thumbnail.
try this with some Modification it may help u..
private void walkdir(File dir)
{
String filePattent = ".jpge";
String filePattentCAP = ".JPGE";
File listFile[] = dir.listFiles();
if (listFile != null)
{
for (int i = 0; i < listFile.length; i++)
{
if (listFile[i].isDirectory())
{
walkdir(listFile[i]);
}
else if (listFile[i].getName().endsWith(filePattent) || listFile[i].getName().endsWith(filePattentCAP))
{
fileName.add(listFile[i].getName());
filePath.add(listFile[i].getAbsolutePath());
}
}
}
}

Categories

Resources