以编程方式在Android中删除*。*本地文件

时间:2019-11-13 09:33:39

标签: android

我正在编写一个在本地存储文件的应用程序。 我可以通过指定文件名

轻松删除文件
void

是否可以通配符删除文件以清空存储空间并清理?编辑:如果是这样,请您告诉我如何做。

1 个答案:

答案 0 :(得分:0)

尝试一下:

 delete whole file:

  File dir = new File(Environment.getExternalStorageDirectory()+"directory_name"); 
if (dir.isDirectory()) 
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
   new File(dir, children[i]).delete();
}
}

OR

  try{
   String d_path = "your file path";
   File file = new File(d_path);
   Boolean isDeleted= delete(context,file );
   }catch(Exception e){
    e.printStackTrace();
 }

public static boolean delete(final Context context, final File file) {
final String where = MediaStore.MediaColumns.DATA + "=?";
final String[] selectionArgs = new String[] {
        file.getAbsolutePath()
};
final ContentResolver contentResolver = context.getContentResolver();
final Uri filesUri = MediaStore.Files.getContentUri("external");

contentResolver.delete(filesUri, where, selectionArgs);

if (file.exists()) {

    contentResolver.delete(filesUri, where, selectionArgs);
}
return !file.exists();
}