示例#1
0
 @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;
 }
  /**
   * Create a folder. The folder may even be on external SD card for Kitkat.
   *
   * @param file The folder to be created.
   * @return True if creation was successful.
   */
  public static boolean mkdir(final File file, Context context) {
    if (file == null) return false;
    if (file.exists()) {
      // nothing to create.
      return file.isDirectory();
    }

    // Try the normal way
    if (file.mkdirs()) {
      return true;
    }

    // Try with Storage Access Framework.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
        && FileUtil.isOnExtSdCard(file, context)) {
      DocumentFile document = getDocumentFile(file, true, context);
      // getDocumentFile implicitly creates the directory.

      return document.exists();
    }

    // Try the Kitkat workaround.
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
      try {
        return MediaStoreHack.mkdir(context, file);
      } catch (IOException e) {
        return false;
      }
    }

    return false;
  }