简体   繁体   中英

Android java : delete selected file / folder

How to delete the selected file / folder by using long pressed ? I'm developing an File Explorer app and there are listed folder and file from my storage. I want to have a delete function for the longpressed() .

public void longpressed(){

this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
        new AlertDialog.Builder(ViewNoteActivity.this , AlertDialog.THEME_HOLO_DARK)
        .setTitle("Delete Folder / File")
        .setMessage("Are you sure you want to delete the selected folder / file ?")
        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which ) { 
                 boolean success = true;


                if (success) {
                    Toast.makeText(getBaseContext(), "You have successfully delete." , Toast.LENGTH_SHORT ).show();



            } else {
                Toast.makeText(getBaseContext(), "You have Failed to delete." , Toast.LENGTH_SHORT ).show();
            }
            }

         })
        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                // do nothing
            }
         })
        .setIcon(R.drawable.ic_launcher)
         .show();
        return true;
    }
});

}

item select coding:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    super.onListItemClick(l, v, position, id);
    FileInfo fileDescriptor = fileArrayListAdapter.getItem(position);
    if (fileDescriptor.isFolder() || fileDescriptor.isParent()) {
        currentFolder = new File(fileDescriptor.getPath());
        fill(currentFolder);
    } else {

        fileSelected = new File(fileDescriptor.getPath());
        Intent intent = new Intent();
        intent.putExtra(Constants.KEY_FILE_SELECTED,
                fileSelected.getAbsolutePath());
        setResult(Activity.RESULT_OK, intent);
        Log.i("FILE CHOOSER", "result ok");
              }
          }

See the File class API reference.

To delete a file:

new File(path).delete()

To delete a folder:

private void deleteFolderRecursive(File dir) {
    File[] files = dir.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.isDirectory()) {
                deleteFolderRecursive(file);
            } else {
                file.delete();
            }
        }
    }

    dir.delete();
}

Do this:

 File dir =new File(getActivity().getApplicationContext().getFilesDir()+"/YourFOlderName");
            boolean success = deleteDir(dir);

Where:

getActivity().getApplicationContext().getFilesDir()+"/YourFOlderName"

is the path to the folder.

And:

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

The above will delete all the children inside the directory.

if your folder is on the External SD Card the path should be mounted like:

Never hardcode the sdcard, you must use

Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)

will let you know if the memory is loaded. Then use:

Environment.getExternalStorageDirectory().getAbsolutePath()

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM