如何删除整个文件夹和内容?
我希望我的应用程序的用户能够删除 DCIM 文件夹(位于 SD 卡上并包含子文件夹)。
这可能吗,如果可以的话怎么办?
I want the users of my application to be able to delete the DCIM folder (which is located on the SD card and contains subfolders).
Is this possible, if so how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
我已经把这个放了,尽管它会删除具有任何目录结构的文件夹。
希望这有帮助。
I've put this one though its' paces it deletes a folder with any directory structure.
Hope this helps.
解决这个问题的另一种(现代)方法。
自 API 26 起在 Android 上
Yet another (modern) way to solve it.
On Android since API 26
我正在使用这个递归函数来完成这项工作:
该函数检查它是目录还是文件。
如果它是一个目录,则检查它是否有子文件,如果有子文件,将再次调用自己传递子文件并重复。
如果它是一个文件,则将其删除。
(不要使用此函数通过传递缓存目录来清除应用程序缓存,因为它也会删除缓存目录,因此应用程序将崩溃......
如果您想清除缓存,请使用此函数,该函数不会删除您传递给它的目录:
或者您可以使用以下方法检查它是否是缓存目录:
清除应用程序缓存的示例代码:
再见,祝您有美好的一天&编码:D
I'm using this recursive function to do the job:
The function checks if it is a directory or a file.
If it is a directory checks if it has child files, if it has child files will call herself again passing the children and repeating.
If it is a file it delete it.
(Don't use this function to clear the app cache by passing the cache dir because it will delete the cache dir too so the app will crash...
If you want to clear the cache you use this function that won't delete the dir you pass to it:
or you can check if it is the cache dir using:
Example code to clear app cache:
Bye, Have a nice day & coding :D
这是 kotlin 选项。效果非常好。
// 避免连续多次调用此函数,它可以删除整个文件夹内容
This is kotlin option. It worked very well.
// avoid calling this multiple times in row, it can delete whole folder contents
您可以像这样递归删除文件和文件夹:
You can delete files and folders recursively like this:
首先我告诉您,您无法删除 DCIM 文件夹,因为它是系统文件夹。当您在手机上手动删除它时,它将删除该文件夹的内容,但不会删除 DCIM 文件夹。您可以使用以下方法删除其内容:
根据评论更新
Let me tell you first thing you cannot delete the DCIM folder because it is a system folder. As you delete it manually on phone it will delete the contents of that folder, but not the DCIM folder. You can delete its contents by using the method below:
Updated as per comments
我们可以使用命令行参数来删除整个文件夹及其内容。
上述代码的用法示例:
We can use the command line arguments to delete a whole folder and its contents.
Example usage of the above code:
在 Kotlin 中,您可以使用
kotlin.io
包中的deleteRecursively()
扩展In Kotlin you can use
deleteRecursively()
extension fromkotlin.io
package简短的 koltin 版本
UPDATE
Kotlin stdlib 函数
Short koltin version
UPDATE
Kotlin stdlib function
使用以下方法删除包含文件及其子目录的整个主目录。调用此方法后,再次调用主目录的delete()目录。
use below method to delete entire main directory which contains files and it's sub directory. After calling this method once again call delete() directory of your main directory.
对于仅包含文件的文件夹,您的方法是不错的,但是如果您正在寻找还包含子文件夹的场景,则需要递归
此外,您还应该捕获返回的返回值,以确保允许您删除文件
并包含
在你的清单中
Your approach is decent for a folder that only contains files, but if you are looking for a scenario that also contains subfolders then recursion is needed
Also you should capture the return value of the return to make sure you are allowed to delete the file
and include
in your manifest
有很多答案,但我决定添加自己的答案,因为它有点不同。它基于 OOP ;)
我创建了 DirectoryCleaner 类,每次当我需要清理某些目录时它都会帮助我。
它可以通过以下方式解决这个问题:
There is a lot of answers, but I decided to add my own, because it's little different. It's based on OOP ;)
I created class DirectoryCleaner, which help me each time when I need to clean some directory.
It can be used to solve this problem in next way:
如果该目录包含 Java 中的子目录或文件,则无法删除该目录。尝试这个两行简单的解决方案。这将删除该目录以及目录内的竞赛。
在 gradle 文件中添加此行并同步项目
You can not delete the directory if it has subdirectories or files in Java. Try this two-line simple solution. This will delete the directory and contests inside the directory.
Add this line in gradle file and sync the project
根据文档:
因此,您应该检查
listFiles
是否为null
,如果不是则继续According to the documentation:
So you should check if
listFiles
isnull
and only continue if it's not如果您不需要递归删除内容,您可以尝试如下操作:
If you dont need to delete things recursively you can try something like this:
请参阅 android.os.FileUtils,它隐藏在 API 21
来源: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/FileUtils.java#414
see android.os.FileUtils, it's hide on API 21
Source: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/FileUtils.java#414
最快、最简单的方法:
The fastest and easiest way:
这就是我所做的......(简洁且经过测试)
This is what I do... (terse and tested)
从目录中删除所有文件的简单方法:
它是通过仅调用
deleteAllImageFile(context);从目录中删除所有图像的通用函数;
Simple way to delete all file from directory :
It is generic function for delete all images from directory by calling only
deleteAllImageFile(context);
我知道的最安全的代码:
检查文件是否存在,处理空值,检查目录是否确实被删除
Safest code I know:
Checks the file exists, handles nulls, checks the directory was actually deleted
这是您的解决方案,它也会刷新图库。
here is your solution it will also refresh the gallery as well.
此(尝试删除所有子文件和子目录,包括提供的目录):
文件
,则删除空目录
,非空目录
,再次调用删除子目录,重复1到3示例:
要获得对外部存储目录的访问权限,您需要以下权限:
(使用
ContextCompat.checkSelfPermission
和ActivityCompat.requestPermissions
)递归方法:
This (Tries to delete all sub-files and sub-directories including the supplied directory):
File
, deleteEmpty Directory
, deleteNot Empty Directory
, call delete again with sub-directory, repeat 1 to 3example:
To gain access to External Storage Directory, you need the following permissions:
(Use
ContextCompat.checkSelfPermission
andActivityCompat.requestPermissions
)Recursive method:
这是一个非递归实现,仅供娱乐:
Here is a non-recursive implementation, just for fun: