我希望我的应用程序的用户能够删除DCIM文件夹(它位于SD卡上并包含子文件夹)。
这可能吗?如果可能,怎么可能?
我希望我的应用程序的用户能够删除DCIM文件夹(它位于SD卡上并包含子文件夹)。
这可能吗?如果可能,怎么可能?
当前回答
看到android.os。FileUtils,它隐藏在API 21上
public static boolean deleteContents(File dir) {
File[] files = dir.listFiles();
boolean success = true;
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
success &= deleteContents(file);
}
if (!file.delete()) {
Log.w("Failed to delete " + file);
success = false;
}
}
}
return success;
}
来源:https://android.googlesource.com/platform/frameworks/base/ + /主/核心/ java / android / os / FileUtils.java # 414
其他回答
private static void deleteRecursive(File dir)
{
//Log.d("DeleteRecursive", "DELETEPREVIOUS TOP" + dir.getPath());
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
File temp = new File(dir, children[i]);
deleteRecursive(temp);
}
}
if (dir.delete() == false)
{
Log.d("DeleteRecursive", "DELETE FAIL");
}
}
让我告诉你第一件事你不能删除DCIM文件夹,因为它是一个系统文件夹。当你在手机上手动删除它时,它会删除该文件夹的内容,但不会删除DCIM文件夹。您可以通过以下方法删除其内容:
根据评论更新
File dir = new File(Environment.getExternalStorageDirectory()+"Dir_name_here");
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
//To delete all the files of a specific folder & subfolder
public static void deleteFiles(File directory, Context c) {
try {
for (File file : directory.listFiles()) {
if (file.isFile()) {
final ContentResolver contentResolver = c.getContentResolver();
String canonicalPath;
try {
canonicalPath = file.getCanonicalPath();
} catch (IOException e) {
canonicalPath = file.getAbsolutePath();
}
final Uri uri = MediaStore.Files.getContentUri("external");
final int result = contentResolver.delete(uri,
MediaStore.Files.FileColumns.DATA + "=?", new String[]{canonicalPath});
if (result == 0) {
final String absolutePath = file.getAbsolutePath();
if (!absolutePath.equals(canonicalPath)) {
contentResolver.delete(uri,
MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
}
}
if (file.exists()) {
file.delete();
if (file.exists()) {
try {
file.getCanonicalFile().delete();
} catch (IOException e) {
e.printStackTrace();
}
if (file.exists()) {
c.deleteFile(file.getName());
}
}
}
} else
deleteFiles(file, c);
}
} catch (Exception e) {
}
}
这是你的解决方案,它也将刷新画廊以及。
我用这个递归函数来做这个工作:
public static void deleteDirAndContents(@NonNull File mFile){
if (mFile.isDirectory() && mFile.listFiles() != null && mFile.listFiles().length > 0x0) {
for (File file : mFile.listFiles()) {
deleteDirAndContents(file);
}
} else {
mFile.delete();
}
}
该函数检查它是目录还是文件。
如果是目录检查它是否有子文件,如果有子文件将再次调用自己传递子文件并重复。
如果它是一个文件,它删除它。
(不要使用这个函数通过传递缓存目录来清除应用程序缓存,因为它也会删除缓存目录,所以应用程序会崩溃… 如果你想清除缓存,你可以使用这个函数,它不会删除你传递给它的dir:
public static void deleteDirContents(@NonNull File mFile){
if (mFile.isDirectory() && mFile.listFiles() != null && mFile.listFiles().length > 0x0) {
for (File file : mFile.listFiles()) {
deleteDirAndContents(file);
}
}
}
或者你可以检查它是否是缓存目录使用:
if (!mFile.getAbsolutePath().equals(context.getCacheDir().getAbsolutePath())) {
mFile.delete();
}
清除应用程序缓存的示例代码:
public static void clearAppCache(Context context){
try {
File cache = context.getCacheDir();
FilesUtils.deleteDirContents(cache);
} catch (Exception e){
MyLogger.onException(TAG, e);
}
}
再见,有一个美好的一天&编码:D
这种方法适用于只包含文件的文件夹,但如果您正在寻找还包含子文件夹的场景,则需要递归
此外,您应该捕获返回的返回值,以确保允许您删除该文件
,包括
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在你的清单上
void DeleteRecursive(File dir)
{
Log.d("DeleteRecursive", "DELETEPREVIOUS TOP" + dir.getPath());
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
File temp = new File(dir, children[i]);
if (temp.isDirectory())
{
Log.d("DeleteRecursive", "Recursive Call" + temp.getPath());
DeleteRecursive(temp);
}
else
{
Log.d("DeleteRecursive", "Delete File" + temp.getPath());
boolean b = temp.delete();
if (b == false)
{
Log.d("DeleteRecursive", "DELETE FAIL");
}
}
}
}
dir.delete();
}