Android java:删除所选文件/文件夹

时间:2014-03-16 09:06:37

标签: java android delete-file

如何使用长按删除所选文件/文件夹? 我正在开发一个文件资源管理器应用程序,并列出了我的存储中的文件夹和文件。 我想要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;
    }
});

}

项目选择编码:

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

2 个答案:

答案 0 :(得分:1)

请参阅File class API参考。

要删除文件:

new File(path).delete()

删除文件夹:

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

答案 1 :(得分:0)

这样做:

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

其中:

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

是文件夹的路径。

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

以上将删除目录中的所有子项。

如果您的文件夹位于外部SD卡上,则路径应安装如下:

永远不要对SD卡进行硬编码,必须使用

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

会告诉您内存是否已加载。然后使用:

Environment.getExternalStorageDirectory().getAbsolutePath()