예제 #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;
 }
예제 #2
0
 private DocumentFile getDCIMDocumentFolder(boolean create) {
   DocumentFile documentFile = null;
   DocumentFile sdDir;
   if ((sdDir = getExternalSdDocumentFile()) != null) {
     documentFile = sdDir.findFile("DCIM");
     if (documentFile == null && create) documentFile = sdDir.createDirectory("DCIM");
   }
   return documentFile;
 }
예제 #3
0
 @Override
 public DocumentFile getFreeDcamDocumentFolder() {
   DocumentFile dcimfolder;
   DocumentFile freedcamfolder = null;
   if ((dcimfolder = getDCIMDocumentFolder(true)) != null) {
     freedcamfolder = dcimfolder.findFile("FreeDcam");
     if (freedcamfolder == null) freedcamfolder = dcimfolder.createDirectory("FreeDcam");
   }
   return freedcamfolder;
 }
  /**
   * Get a DocumentFile corresponding to the given file (for writing on ExtSdCard on Android 5). If
   * the file is not existing, it is created.
   *
   * @param file The file.
   * @param isDirectory flag indicating if the file should be a directory.
   * @return The DocumentFile
   */
  public static DocumentFile getDocumentFile(
      final File file, final boolean isDirectory, Context context) {
    String baseFolder = getExtSdCardFolder(file, context);
    boolean originalDirectory = false;
    if (baseFolder == null) {
      return null;
    }

    String relativePath = null;
    try {
      String fullPath = file.getCanonicalPath();
      if (!baseFolder.equals(fullPath)) relativePath = fullPath.substring(baseFolder.length() + 1);
      else originalDirectory = true;
    } catch (IOException e) {
      return null;
    } catch (Exception f) {
      originalDirectory = true;
      // continue
    }
    String as = PreferenceManager.getDefaultSharedPreferences(context).getString("URI", null);

    Uri treeUri = null;
    if (as != null) treeUri = Uri.parse(as);
    if (treeUri == null) {
      return null;
    }

    // start with root of SD card and then parse through document tree.
    DocumentFile document = DocumentFile.fromTreeUri(context, treeUri);
    if (originalDirectory) return document;
    String[] parts = relativePath.split("\\/");
    for (int i = 0; i < parts.length; i++) {
      DocumentFile nextDocument = document.findFile(parts[i]);

      if (nextDocument == null) {
        if ((i < parts.length - 1) || isDirectory) {
          nextDocument = document.createDirectory(parts[i]);
        } else {
          nextDocument = document.createFile("image", parts[i]);
        }
      }
      document = nextDocument;
    }

    return document;
  }