Is there a command that would delete stored application data? I've searched the Forum without success. Any advice on this would be greatly appreciated.
:sign0085:
:sign0085:
Thank you for your reply. I'm hoping to find something more generic than deleting a list of files. Something like flushing a cache, but instead clearing stored data. Do you or anyone else have any other suggestions?You can create a list of the files in the directory you want to delete then use a for loop to go through the list and delete them.
package com.hrupin.cleaner;
import java.io.File;
import android.app.Application;
import android.util.Log;
public class MyApplication extends Application {
private static MyApplication instance;
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
public static MyApplication getInstance() {
return instance;
}
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();
}
}
Hello jonPM – I could do something like the code below, but I would have to know where the files were stored. I would need some means of determining if the files were in DirInternal or DirExternal, and I don’t know how to do this. At this point, it’s the details that are eluding me. Does this seem like a reasonable way to start?What I meant is that Pluton's java code does exactly what I've described. It looks in the apps directory, makes a list of the files in it, then deletes them one by one. You don't need a library to do this.
Dim FilesToDelete As List
FilesToDelete.Initialize
FilesToDelete.AddAll(File.ListFiles(File.DirInternal))
For I = 0 To FilesToDelete.Size -1
File.Delete(File.DirInternal, FilesToDelete.Get(I))
Next
Thank you jonPM - Depending on the resources available, my app will install directly to the device or an sd card. This is why I need to verify if an sd card exists. The code below is what I have come up with, but I get the error “Unknown member: externalwriteable” at line: If File.ExternalWriteable = True Then. Do you know if a library is required to use this means of verifying an sd card?Yes, that's what you need to do. You should know where the files are since you are writing them elsewhere in your code. If you are using multiple locations then just repeat the code accordingly.
Sub ClearStoredData
Dim DefaultDir As String
Dim FilesToDelete As List
If File.ExternalWriteable = True Then
DefaultDir = "DirExternal"
Else
DefaultDir = "DirInternal"
End If
FilesToDelete.Initialize
FilesToDelete.AddAll(File.ListFiles(File.DefaultDir))
For I = 0 To FilesToDelete.Size -1
File.Delete(File.DefaultDir, FilesToDelete.Get(I))
Next
End Sub
Sub ClearStoredData
Dim DefaultDir As String
Dim FilesToDelete As List
If File.ExternalWriteable = True Then
DefaultDir = File.DirDefaultExternal
Else
DefaultDir = File.DirInternal
End If
FilesToDelete.Initialize
FilesToDelete.AddAll(File.ListFiles(DefaultDir))
For I = 0 To FilesToDelete.Size -1
File.Delete(DefaultDir, FilesToDelete.Get(I))
Next
End Sub
Thank you jonMP - you have been most helpful. I now have things sorted out. For anyone interested in this post, I had a typo that generated the “Unknown member: externalwriteable” error. I incorrectly spelled Writable as Writeable. Silly me! :sign0161:That line looks correct, but you can't add a member to File (with DefaultDir) in that manner. instead do this:
B4X:Sub ClearStoredData Dim DefaultDir As String Dim FilesToDelete As List If File.ExternalWriteable = True Then DefaultDir = File.DirDefaultExternal Else DefaultDir = File.DirInternal End If FilesToDelete.Initialize FilesToDelete.AddAll(File.ListFiles(DefaultDir)) For I = 0 To FilesToDelete.Size -1 File.Delete(DefaultDir, FilesToDelete.Get(I)) Next End Sub