List of usage examples for android.support.v4.provider DocumentFile createDirectory
public abstract DocumentFile createDirectory(String str);
From source file:Main.java
public static DocumentFile newDirectory(DocumentFile parentDir, String base) { int postfix = 0; do {/*w w w . j a v a2s . com*/ String name = base; if (postfix > 0) { name += "_" + postfix; } if (parentDir.findFile(name) == null) { return parentDir.createDirectory(name); } postfix++; } while (true); }
From source file:com.osama.cryptofm.utils.FileDocumentUtils.java
public static DocumentFile getDocumentFile(final File file) { String baseFolder = SharedData.EXTERNAL_SDCARD_ROOT_PATH; boolean isDirectory = file.isDirectory(); String relativePath = null;/*from w w w .j a v a2s . c o m*/ try { Log.d(TAG, "getDocumentFile: basefolder:file" + baseFolder + " : " + file.getAbsolutePath()); String fullPath = file.getCanonicalPath(); if ((file.getAbsolutePath() + "/").equals(baseFolder)) { Log.d(TAG, "getDocumentFile: uri is: " + SharedData.EXT_ROOT_URI); return DocumentFile.fromTreeUri(CryptoFM.getContext(), Uri.parse(SharedData.EXT_ROOT_URI)); } else { relativePath = fullPath.substring(baseFolder.length()); } } catch (IOException e) { return null; } Uri treeUri = Uri.parse(SharedData.EXT_ROOT_URI); if (treeUri == null) { return null; } relativePath = relativePath.replace(SharedData.EXTERNAL_SDCARD_ROOT_PATH, ""); // start with root of SD card and then parse through document tree. Log.d(TAG, "getDocumentFile: relative path is: " + relativePath); DocumentFile document = DocumentFile.fromTreeUri(CryptoFM.getContext(), treeUri); String[] parts = relativePath.split("\\/"); for (int i = 0; i < parts.length; i++) { DocumentFile nextDocument = document.findFile(parts[i]); Log.d(TAG, "getDocumentFile: next document is: " + parts[i]); if (nextDocument == null) { if ((i < parts.length - 1) || isDirectory) { Log.d(TAG, "getDocumentFile: creating next directory"); nextDocument = document.createDirectory(parts[i]); } else { nextDocument = document.createFile("image", parts[i]); } } document = nextDocument; } Log.d(TAG, "getDocumentFile: file uri is: " + document.getUri()); return document; }
From source file:com.frostwire.android.StoragePicker.java
public static String handle(Context context, int requestCode, int resultCode, Intent data) { String result = null;//from ww w.j a v a 2 s. com try { if (resultCode == Activity.RESULT_OK && requestCode == SELECT_FOLDER_REQUEST_CODE) { Uri treeUri = data.getData(); ContentResolver cr = context.getContentResolver(); final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); cr.takePersistableUriPermission(treeUri, takeFlags); if (treeUri == null) { UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_null); result = null; } else { DocumentFile file = DocumentFile.fromTreeUri(context, treeUri); if (!file.isDirectory()) { UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_not_directory); result = null; } else if (!file.canWrite()) { UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_cant_write); result = null; } else { LollipopFileSystem fs = (LollipopFileSystem) Platforms.fileSystem(); result = fs.getTreePath(treeUri); if (result != null && !result.endsWith("/FrostWire")) { DocumentFile f = file.findFile("FrostWire"); if (f == null) { file.createDirectory("FrostWire"); } } } } } } catch (Throwable e) { UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_error); LOG.error("Error handling folder selection", e); result = null; } if (result != null) { ConfigurationManager.instance().setStoragePath(result); BTEngine.ctx.dataDir = Platforms.data(); BTEngine.ctx.torrentsDir = Platforms.torrents(); } return result; }
From source file:org.dkf.jed2k.android.LollipopFileSystem.java
private static DocumentFile getDirectory(Context context, File dir, boolean create) { try {//from w ww. j ava 2 s . c om 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.theelix.libreexplorer.FileManager.java
public static void createDestFiles(DocumentFile folder) throws IOException, FileNotCreatedException { ArrayList<String> destFiles = new ArrayList<>(); for (File selectedFile : selectedFiles) { String origFileName;//from ww w. j av a 2 s . c om String extension; try { origFileName = selectedFile.getName().substring(0, selectedFile.getName().lastIndexOf(".")); } catch (IndexOutOfBoundsException e) { origFileName = selectedFile.getName(); } String fileName = selectedFile.getName(); try { extension = fileName.substring(fileName.lastIndexOf(".")); } catch (IndexOutOfBoundsException e) { extension = ""; } int lastIndex = 1; while (new File(getCurrentDirectory() + File.separator + fileName).exists()) { fileName = origFileName + " (" + lastIndex + ")" + extension; lastIndex++; } if (selectedFile.isDirectory()) { folder.createDirectory(fileName); } else { folder.createFile(FileUtilties.getMimeType(selectedFile), fileName); } destFiles.add(fileName); } FileManager.destFiles = destFiles.toArray(new String[destFiles.size()]); PasteTask task = new PasteTask(mContext); task.execute(); }
From source file:com.frostwire.android.LollipopFileSystem.java
private static DocumentFile getDirectory(Context context, File dir, boolean create) { try {/*www . jav a 2 s . c om*/ String path = dir.getAbsolutePath(); DocumentFile cached = CACHE.get(path); if (cached != null && cached.isDirectory()) { return cached; } 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.split("/"); Uri rootUri = getDocumentUri(context, new File(baseFolder)); DocumentFile f = DocumentFile.fromTreeUri(context, rootUri); // special FrostWire case if (create) { if (baseFolder.endsWith("/FrostWire") && !f.exists()) { baseFolder = baseFolder.substring(0, baseFolder.length() - 10); rootUri = getDocumentUri(context, new File(baseFolder)); f = DocumentFile.fromTreeUri(context, rootUri); f = f.findFile("FrostWire"); if (f == null) { f = f.createDirectory("FrostWire"); if (f == null) { return null; } } } } for (String segment : segments) { 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; } } } f = f.isDirectory() ? f : null; if (f != null) { CACHE.put(path, f); } return f; } catch (Throwable e) { LOG.error("Error getting directory: " + dir, e); return null; } }
From source file:com.osama.cryptofm.tasks.MoveTask.java
private void move(File f) throws Exception { if (f.isDirectory()) { //check if destination folder is in external sdcard if (FileUtils.isDocumentFile(mDestinationFolder)) { DocumentFile rootDocumentFile = FileDocumentUtils.getDocumentFile(new File(mDestinationFolder)); rootDocumentFile.createDirectory(f.getName()); } else {/*from www .java 2s. c o m*/ //create folder in the destination mDestinationFolder = mDestinationFolder + f.getName() + "/"; File tmp = new File(mDestinationFolder); if (!tmp.exists()) { tmp.mkdir(); } else { return; } } Log.d(TAG, "move: File is a directory"); for (File file : f.listFiles()) { move(file); } } else { Log.d(TAG, "move: Moving file: " + f.getName()); Log.d(TAG, "move: Destination folder is: " + mDestinationFolder); isNextFile = true; publishProgress(f.getName()); publishProgress("" + 0); if (FileUtils.isDocumentFile(mDestinationFolder)) { DocumentFile dest = rootDocumentFile.createFile(FileUtils.getExtension(f.getName()), f.getName()); innerMove(new BufferedInputStream(new FileInputStream(f)), CryptoFM.getContext().getContentResolver().openOutputStream(dest.getUri()), f.length()); } else { //check if file already exits File destinationFile = new File(mDestinationFolder + f.getName()); if (destinationFile.exists()) { return; } innerMove(new BufferedInputStream(new FileInputStream(f)), new BufferedOutputStream(new FileOutputStream(destinationFile)), f.length()); } } //delete the input file //if copying then don't if (SharedData.IS_COPYING_NOT_MOVING) { return; } if (!f.delete()) { throw new IOException("cannot move files"); } }
From source file:com.filemanager.free.filesystem.FileUtil.java
/** * Get a DocumentFile corresponding to the given file (for writing on ExtSdCard on Android 5). If the file is not * existing, it is created./*ww w.j a va 2 s .c o m*/ * * @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; }
From source file:freed.ActivityAbstract.java
private DocumentFile getDCIMDocumentFolder(boolean create) { DocumentFile documentFile = null;// w w w. j a va 2 s .c o m DocumentFile sdDir; if ((sdDir = getExternalSdDocumentFile()) != null) { documentFile = sdDir.findFile("DCIM"); if (documentFile == null && create) documentFile = sdDir.createDirectory("DCIM"); } return documentFile; }
From source file:freed.ActivityAbstract.java
@Override public DocumentFile getFreeDcamDocumentFolder() { DocumentFile dcimfolder; DocumentFile freedcamfolder = null;// w ww . j ava 2 s . c o m if ((dcimfolder = getDCIMDocumentFolder(true)) != null) { freedcamfolder = dcimfolder.findFile("FreeDcam"); if (freedcamfolder == null) freedcamfolder = dcimfolder.createDirectory("FreeDcam"); } return freedcamfolder; }