Fay Zan Fay Zan - 3 months ago 18
Java Question

delete multiple checked files from internal storage

TL;DR

How to delete multiple files, file names got from

util.myPos





FULL EXPLANATION

I can delete one file by using
deleteFile("fileName")
method

But how do I delete multiple files at once?

All the names are listed in an
ArrayList


Java code to check/uncheck while adding file name to
ArrayList


OnItemClickListener getFileEditContent = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String clickedFile = (String)parent.getItemAtPosition(position);
setMultiEdit(clickedFile);

// change the checkbox state
CheckedTextView checkedTextView = ((CheckedTextView) view);
if (checkedTextView.isChecked()) {
((CheckedTextView)view).setChecked(true);
checkedTextView.setChecked(!checkedTextView.isChecked());

utils.myPos.remove(stringEditData);

count--;
if(count <= 1){
multiEdit.setEnabled(true);
}

} else{
checkedTextView.setChecked(!checkedTextView.isChecked());
utils.myPos.add(stringEditData);

count++;
if(count >= 2){
multiEdit.setEnabled(false);
}

}
}
};


Codes to delete the file

void setMultiDelete() {
multiDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (utils.myPosTitle == null || utils.myPosTitle.isEmpty()) {
Toast.makeText(addressActivity.this, "Select address", Toast.LENGTH_SHORT).show();
} else {
String content = utils.myPosTitle.toString();
String cutContent = content.substring(content.indexOf('[') + 1, content.lastIndexOf(']'));
deleteFile(cutContent);

Toast.makeText(addressActivity.this, "Deleted", Toast.LENGTH_SHORT).show();
showDirFile(dataDr);
utils.myPos.clear();
count = 0;
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(addressActivity.this,
android.R.layout.simple_list_item_multiple_choice,
SavedFiles);
listView.setAdapter(adapter);
}

}
});
}


Note This code only deletes one file

Answer

I had the same problem before, there is no method or utility in java that can delete multiple file in the same time, the only way that I could find was deleting them one by one in a loop. because of that some parts of your code in unclear for me (setMultiEdit and utils.myPosTitle) I can not give you a code solution that be useful. but there are many threads in stackoverflow that will be help you for modifying your code.

Sample code

private void cleaner(File directory) {

            if (directory == null)
                return;
            File[] tempDirectory = directory.listFiles();
            if (tempDirectory == null)
                return;
            for (File file : tempDirectory) {
                if (file.isFile()) {
                    if (file.delete())
                        deleteFileCount++;
                } else { // if it's directory
                    cleaner(file);
                    if (file.listFiles() != null)
                        if (file.listFiles().length == 0) // if the directory is empty (after deleting inner files)
                            if (file.delete())
                                deleteFolderCount++;
                }
            }
}

this is my function that I use it for deleting all files and directories within a given directory in method parameter.

I hope it's gives you an idea to modify your code for your situation. :)

Comments