@Nullable
 private boolean deletFile(File file) {
   if (!file.delete()) {
     DocumentFile sdDir = getExternalSdDocumentFile();
     if (sdDir == null) return false;
     String baseS = sdDir.getName();
     String fileFolder = file.getAbsolutePath();
     String[] split = fileFolder.split("/");
     DocumentFile tmpdir = null;
     boolean append = false;
     for (String aSplit : split) {
       if (aSplit.equals(baseS) || append) {
         if (!append) {
           append = true;
           tmpdir = sdDir;
         } else {
           tmpdir = tmpdir.findFile(aSplit);
         }
       }
     }
     boolean d = false;
     d = !(tmpdir != null && tmpdir.exists()) || tmpdir.delete();
     Logger.d("delteDocumentFile", "file delted:" + d);
     return d;
   }
   return true;
 }
  /**
   * Check for a directory if it is possible to create files within this directory, either via
   * normal writing or via Storage Access Framework.
   *
   * @param folder The directory
   * @return true if it is possible to write in this directory.
   */
  public static final boolean isWritableNormalOrSaf(final File folder, Context c) {
    // Verify that this is a directory.
    if (folder == null) return false;
    if (!folder.exists() || !folder.isDirectory()) {
      return false;
    }

    // Find a non-existing file in this directory.
    int i = 0;
    File file;
    do {
      String fileName = "AugendiagnoseDummyFile" + (++i);
      file = new File(folder, fileName);
    } while (file.exists());

    // First check regular writability
    if (isWritable(file)) {
      return true;
    }

    // Next check SAF writability.
    DocumentFile document = getDocumentFile(file, false, c);

    if (document == null) {
      return false;
    }

    // This should have created the file - otherwise something is wrong with access URL.
    boolean result = document.canWrite() && file.exists();

    // Ensure that the dummy file is not remaining.
    document.delete();

    return result;
  }
  /**
   * Delete a file. May be even on external SD card.
   *
   * @param file the file to be deleted.
   * @return True if successfully deleted.
   */
  public static final boolean deleteFile(@NonNull final File file, Context context) {
    // First try the normal deletion.
    boolean fileDelete = deleteFilesInFolder(file, context);
    if (file.delete() || fileDelete) return true;

    // Try with Storage Access Framework.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
        && FileUtil.isOnExtSdCard(file, context)) {

      DocumentFile document = getDocumentFile(file, false, context);
      return document.delete();
    }

    // Try the Kitkat workaround.
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
      ContentResolver resolver = context.getContentResolver();

      try {
        Uri uri = MediaStoreUtil.getUriFromFile(file.getAbsolutePath(), context);
        resolver.delete(uri, null, null);
        return !file.exists();
      } catch (Exception e) {
        Log.e("AmazeFileUtils", "Error when deleting file " + file.getAbsolutePath(), e);
        return false;
      }
    }

    return !file.exists();
  }
  public static boolean rmdir(final File file, Context context) {
    if (file == null) return false;
    if (!file.exists()) {
      return true;
    }
    if (!file.isDirectory()) {
      return false;
    }
    String[] fileList = file.list();
    if (fileList != null && fileList.length > 0) {
      //  empty the folder.
      rmdir1(file, context);
    }
    String[] fileList1 = file.list();
    if (fileList1 != null && fileList1.length > 0) {
      // Delete only empty folder.
      return false;
    }
    // Try the normal way
    if (file.delete()) {
      return true;
    }

    // Try with Storage Access Framework.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      DocumentFile document = getDocumentFile(file, true, context);
      return document.delete();
    }

    // Try the Kitkat workaround.
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
      ContentResolver resolver = context.getContentResolver();
      ContentValues values = new ContentValues();
      values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
      resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

      // Delete the created entry, such that content provider will delete the file.
      resolver.delete(
          MediaStore.Files.getContentUri("external"),
          MediaStore.MediaColumns.DATA + "=?",
          new String[] {file.getAbsolutePath()});
    }

    return !file.exists();
  }