List of usage examples for android.support.v4.provider DocumentFile isDirectory
public abstract boolean isDirectory();
From source file:com.osama.cryptofm.tasks.MoveTask.java
private void moveDocumentFile(DocumentFile file) throws Exception { if (file.isDirectory()) { Log.d(TAG, "moveDocumentFile: Yes document file is directory"); //change the destination folder mDestinationFolder = mDestinationFolder + file.getName() + "/"; //check if destination folder is not document file if (FileUtils.isDocumentFile(mDestinationFolder)) { rootDocumentFile = rootDocumentFile.createDirectory(file.getName()); } else {//from w w w . j ava2 s.co m File tmp = new File(mDestinationFolder); if (!tmp.exists()) { tmp.mkdir(); } else { return; } } for (DocumentFile f : file.listFiles()) { moveDocumentFile(f); } } else { isNextFile = true; publishProgress(file.getName()); publishProgress("" + 0); //check if pasting in internal storage if (!FileUtils.isDocumentFile(mDestinationFolder)) { Log.d(TAG, "moveDocumentFile: moving document file in internal storage"); File destinationFile = new File(mDestinationFolder + file.getName()); innerMove(CryptoFM.getContext().getContentResolver().openInputStream(file.getUri()), new BufferedOutputStream(new FileOutputStream(destinationFile)), file.length()); } else { Log.d(TAG, "moveDocumentFile: Moving document file honey"); DocumentFile destFile = rootDocumentFile.createFile(file.getType(), file.getName()); innerMove(CryptoFM.getContext().getContentResolver().openInputStream(file.getUri()), CryptoFM.getContext().getContentResolver().openOutputStream(destFile.getUri()), file.length()); } } //delete the input file //if copying then don't if (SharedData.IS_COPYING_NOT_MOVING) { return; } if (!file.delete()) { throw new IOException("cannot move files"); } }
From source file:com.osama.cryptofm.tasks.EncryptTask.java
private void encryptDocumentFile(DocumentFile file) throws FileNotFoundException { Log.d(TAG, "encryptDocumentFile: encrypting document file"); if (file.isDirectory()) { rootDocumentFile = file;/* w w w . jav a 2 s .c om*/ for (DocumentFile f : file.listFiles()) { encryptDocumentFile(f); } } else { //check if root document is not null if (rootDocumentFile == null) { String path = mFilePaths.get(0).substring(0, mFilePaths.get(0).lastIndexOf('/')); Log.d(TAG, "encryptDocumentFile: Getting the root document: " + path); rootDocumentFile = FileDocumentUtils.getDocumentFile(new File(path)); } DocumentFile temp = rootDocumentFile.createFile("pgp", file.getName() + ".pgp"); mCreatedDocumentFiles.add(temp); publishProgress(file.getName(), "" + ((FileUtils.getReadableSize((file.length()))))); InputStream in = CryptoFM.getContext().getContentResolver().openInputStream(file.getUri()); OutputStream out = CryptoFM.getContext().getContentResolver().openOutputStream(temp.getUri()); DocumentFileEncryption.encryptFile(in, out, pubKeyFile, true, new Date(file.lastModified()), file.getName()); } }
From source file:org.dkf.jed2k.android.LollipopFileSystem.java
private static DocumentFile getDirectory(Context context, File dir, boolean create) { try {//from w ww . ja v a 2 s . c o m String path = dir.getAbsolutePath(); String baseFolder = getExtSdCardFolder(context, dir); if (baseFolder == null) { if (create) { return dir.mkdirs() ? DocumentFile.fromFile(dir) : null; } else { return dir.isDirectory() ? DocumentFile.fromFile(dir) : null; } } baseFolder = combineRoot(baseFolder); String fullPath = dir.getAbsolutePath(); String relativePath = baseFolder.length() < fullPath.length() ? fullPath.substring(baseFolder.length() + 1) : ""; String[] segments = relativePath.isEmpty() ? new String[0] : relativePath.split("/"); Uri rootUri = getDocumentUri(context, new File(baseFolder)); DocumentFile f = DocumentFile.fromTreeUri(context, rootUri); for (int i = 0; i < segments.length; i++) { String segment = segments[i]; DocumentFile child = f.findFile(segment); if (child != null) { f = child; } else { if (create) { f = f.createDirectory(segment); if (f == null) { return null; } } else { return null; } } } return f.isDirectory() ? f : null; } catch (Exception e) { LOG.error("Error getting directory: {} {}", dir, e); return null; } }
From source file:com.osama.cryptofm.tasks.DecryptTask.java
private ArrayList<String> getOnlyEncryptedDocumentFiles(ArrayList<String> files) { int size = files.size(); for (int i = 0; i < size; i++) { DocumentFile file = FileDocumentUtils.getDocumentFile(new File(files.get(i))); //check if file is directory assert file != null; if (file.isDirectory()) { //get all the lists of files in directory ArrayList<String> tmp = new ArrayList<>(); for (DocumentFile f : file.listFiles()) { tmp.add(FileUtils.CURRENT_PATH + f.getName()); }/*from ww w . j a va 2s .c o m*/ //recursively get files getOnlyEncryptedDocumentFiles(tmp); } if (FileUtils.isEncryptedFile(mFilePaths.get(i))) { tmp.add(mFilePaths.get(i)); } } //if there are no encrypted file if (tmp.size() < 1) { throw new IllegalArgumentException("No encrypted files found."); } return tmp; }
From source file:com.osama.cryptofm.tasks.DecryptTask.java
private void decryptDocumentFiles(DocumentFile f) throws Exception { Log.d(TAG, "decryptDocumentFiles: Running decryption on document file"); //first always check if task is canceled if (!isCancelled()) { if (f.isDirectory()) { for (DocumentFile tmpFile : f.listFiles()) { decryptDocumentFiles(tmpFile); }/*from w ww .ja v a 2s .c om*/ } else { File out = new File(rootPath + "/", f.getName().substring(0, f.getName().lastIndexOf('.'))); // add the file in created files. top remove the files later of user cancels the task mCreatedFiles.add(out.getAbsoluteFile()); publishProgress(f.getName(), "" + ((FileUtils.getReadableSize((f.length()))))); DocumentFileEncryption.decryptFile( CryptoFM.getContext().getContentResolver().openInputStream(f.getUri()), getSecretKey(), mKeyPass, new BufferedOutputStream(new FileOutputStream(out)) ); } } }
From source file:com.xperia64.timidityae.Globals.java
public static void tryToDeleteFile(Context c, String filename) { filename = filename.replace("//", "/"); if (new File(filename).exists()) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Globals.theFold != null) { DocumentFile df = DocumentFile.fromTreeUri(c, theFold); String split[] = filename.split("/"); int i; for (i = 0; i < split.length; i++) { if (split[i].equals(df.getName())) { i++;/*from w ww . jav a2 s .c o m*/ break; } } DocumentFile xx = df; while (i < split.length) { xx = xx.findFile(split[i++]); //upper.append("../"); if (xx == null) { Log.e("TimidityAE Globals", "Delete file error."); break; } } // Why on earth is DocumentFile's delete method recursive by default? // Seriously. I wiped my sd card twice because of this. if (xx != null && xx.isFile() && !xx.isDirectory()) { xx.delete(); } } else { new File(filename).delete(); } } }
From source file:com.kanedias.vanilla.audiotag.PluginService.java
/** * Finds needed file through Document API for SAF. It's not optimized yet - you can still gain wrong URI on * files such as "/a/b/c.mp3" and "/b/a/c.mp3", but I consider it complete enough to be usable. * @param currentDir - document file representing current dir of search * @param remainingPathSegments - path segments that are left to find * @return URI for found file. Null if nothing found. *//*from w w w . ja v a 2 s .c o m*/ @Nullable private Uri findInDocumentTree(DocumentFile currentDir, List<String> remainingPathSegments) { for (DocumentFile file : currentDir.listFiles()) { int index = remainingPathSegments.indexOf(file.getName()); if (index == -1) { continue; } if (file.isDirectory()) { remainingPathSegments.remove(file.getName()); return findInDocumentTree(file, remainingPathSegments); } if (file.isFile() && index == remainingPathSegments.size() - 1) { // got to the last part return file.getUri(); } } return null; }
From source file:com.frostwire.android.LollipopFileSystem.java
@Override public boolean isDirectory(File file) { if (file.isDirectory()) { return true; }// www . j av a 2 s . c o m DocumentFile f = getDocument(app, file); return f != null && f.isDirectory(); }
From source file:de.arcus.playmusiclib.PlayMusicManager.java
/** * Exports a track to the sd card/*from w w w. j ava 2 s . c o m*/ * @param musicTrack The music track you want to export * @param uri The document tree * @return Returns whether the export was successful */ public boolean exportMusicTrack(MusicTrack musicTrack, Uri uri, String path) { // Check for null if (musicTrack == null) return false; String srcFile = musicTrack.getSourceFile(); // Could not find the source file if (srcFile == null) return false; String fileTmp = getTempPath() + "/tmp.mp3"; // Copy to temp path failed if (!SuperUserTools.fileCopy(srcFile, fileTmp)) return false; // Encrypt the file if (musicTrack.isEncoded()) { String fileTmpCrypt = getTempPath() + "/crypt.mp3"; // Encrypts the file if (trackEncrypt(musicTrack, fileTmp, fileTmpCrypt)) { // Remove the old tmp file FileTools.fileDelete(fileTmp); // New tmp file fileTmp = fileTmpCrypt; } else { Logger.getInstance().logWarning("ExportMusicTrack", "Encrypting failed! Continue with decrypted file."); } } String dest; Uri copyUri = null; if (uri.toString().startsWith("file://")) { // Build the full path dest = uri.buildUpon().appendPath(path).build().getPath(); String parentDirectory = new File(dest).getParent(); FileTools.directoryCreate(parentDirectory); } else { // Complex uri (Lollipop) dest = getTempPath() + "/final.mp3"; // The root DocumentFile document = DocumentFile.fromTreeUri(mContext, uri); // Creates the subdirectories String[] directories = path.split("\\/"); for (int i = 0; i < directories.length - 1; i++) { String directoryName = directories[i]; boolean found = false; // Search all sub elements for (DocumentFile subDocument : document.listFiles()) { // Directory exists if (subDocument.isDirectory() && subDocument.getName().equals(directoryName)) { document = subDocument; found = true; break; } } if (!found) { // Create the directory document = document.createDirectory(directoryName); } } // Gets the filename String filename = directories[directories.length - 1]; for (DocumentFile subDocument : document.listFiles()) { // Directory exists if (subDocument.isFile() && subDocument.getName().equals(filename)) { // Delete the file subDocument.delete(); break; } } // Create the mp3 file document = document.createFile("music/mp3", filename); // Create the directories copyUri = document.getUri(); } // We want to export the ID3 tags if (mID3Enable) { // Adds the meta data if (!trackWriteID3(musicTrack, fileTmp, dest)) { Logger.getInstance().logWarning("ExportMusicTrack", "ID3 writer failed! Continue without ID3 tags."); // Failed, moving without meta data if (!FileTools.fileMove(fileTmp, dest)) { Logger.getInstance().logError("ExportMusicTrack", "Moving the raw file failed!"); // Could not copy the file return false; } } } else { // Moving the file if (!FileTools.fileMove(fileTmp, dest)) { Logger.getInstance().logError("ExportMusicTrack", "Moving the raw file failed!"); // Could not copy the file return false; } } // We need to copy the file to a uri if (copyUri != null) { // Lollipop only if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { try { // Gets the file descriptor ParcelFileDescriptor parcelFileDescriptor = mContext.getContentResolver() .openFileDescriptor(copyUri, "w"); // Gets the output stream FileOutputStream fileOutputStream = new FileOutputStream( parcelFileDescriptor.getFileDescriptor()); // Gets the input stream FileInputStream fileInputStream = new FileInputStream(dest); // Copy the stream FileTools.fileCopy(fileInputStream, fileOutputStream); // Close all streams fileOutputStream.close(); fileInputStream.close(); parcelFileDescriptor.close(); } catch (FileNotFoundException e) { Logger.getInstance().logError("ExportMusicTrack", "File not found!"); // Could not copy the file return false; } catch (IOException e) { Logger.getInstance().logError("ExportMusicTrack", "Failed to write the document: " + e.toString()); // Could not copy the file return false; } } } // Delete temp files cleanUp(); // Adds the file to the media system //new MediaScanner(mContext, dest); // Done return true; }