In my android code, the deleteRecursive method seems to work fine, but when i keep the code inside onPostExecute method, it shows error, saying
cannot resolve method isDirectory(), method call expected in deleteRecursive(child) cannot resolve method delete()
here is my piece of code,
@Override
protected void onPostExecute(JSONObject json) {
super.onPostExecute(json);
int success = 0;
if (pDialog != null && pDialog.isShowing()) {
pDialog.dismiss();
}
if (json != null) {
try {
success = json.getInt(TAG_SUCCESS);
} catch (JSONException e) {
e.printStackTrace();
}
}
if (success == 1) {
String fileOrDirectory = finalSavePath + "/";
public void deleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory()) {
for (File child : fileOrDirectory.listFiles()) {
deleteRecursive(child);
}
fileOrDirectory.delete();
}
} else {
Toast.makeText(context, "Sorry, cannot Delete", Toast.LENGTH_LONG).show();
}
}
}
I understand that there is something very silly which i am missing, nut any help would be greatly appreciated.
you can not make a method inside your if else statement
Try this
public static void deleteFilesAndFolders(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
deleteFilesAndFolders(child);
fileOrDirectory.delete();
}
call deleteFilesAndFolders(fileOrDirectory) directly in your statement.
You have created function inside function, that's the problem.
Try with changing your code as below...
@Override
protected void onPostExecute(JSONObject json) {
super.onPostExecute(json);
int success = 0;
if (pDialog != null && pDialog.isShowing()) {
pDialog.dismiss();
}
if (json != null) {
try {
success = json.getInt(TAG_SUCCESS);
} catch (JSONException e) {
e.printStackTrace();
}
}
if (success == 1) {
String fileOrDirectory = finalSavePath + "/";
deleteRecursive(new File(fileOrDirectory));
}else{
Toast.makeText(context, "Sorry, cannot Delete", Toast.LENGTH_LONG).show();
}
}
public void deleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
deleteRecursive(child);
fileOrDirectory.delete();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.