[英]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()
. 我想为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. 请参阅文件类 API参考。
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: 如果您的文件夹位于外部SD卡上,则路径的安装方式应为:
Never hardcode the sdcard, you must use 切勿对sdcard进行硬编码,必须使用
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
will let you know if the memory is loaded. 会告诉您是否已加载内存。 Then use: 然后使用:
Environment.getExternalStorageDirectory().getAbsolutePath()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.