List of usage examples for android.support.v4.provider DocumentFile findFile
public DocumentFile findFile(String str)
From source file:Main.java
public static DocumentFile newDirectory(DocumentFile parentDir, String base) { int postfix = 0; do {//from w ww .ja v a2 s. c o m 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 .ja va 2 s . co 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 . ja v a 2s .co m 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 getDocument(Context context, File file) { try {//from w ww.j av a 2s . c o m String path = file.getAbsolutePath(); String baseFolder = getExtSdCardFolder(context, file); if (baseFolder == null) { return file.exists() ? DocumentFile.fromFile(file) : null; } //baseFolder = combineRoot(baseFolder); String fullPath = file.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 { return null; } } return f; } catch (Exception e) { LOG.error("Error getting document: {} {}", file, e); return null; } }
From source file:com.frostwire.android.LollipopFileSystem.java
private static DocumentFile getDocument(Context context, File file) { try {// w w w . jav a 2 s.c om String path = file.getAbsolutePath(); DocumentFile cached = CACHE.get(path); if (cached != null) { return cached; } String baseFolder = getExtSdCardFolder(context, file); if (baseFolder == null) { return file.exists() ? DocumentFile.fromFile(file) : null; } baseFolder = combineRoot(baseFolder); String fullPath = file.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); for (String segment : segments) { DocumentFile child = f.findFile(segment); if (child != null) { f = child; } else { return null; } } if (f != null) { CACHE.put(path, f); } return f; } catch (Throwable e) { LOG.error("Error getting document: " + file, e); return null; } }
From source file:org.dkf.jed2k.android.LollipopFileSystem.java
private static DocumentFile getDirectory(Context context, File dir, boolean create) { try {// w w w . ja va 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:org.dkf.jed2k.android.LollipopFileSystem.java
private static DocumentFile getFile(Context context, File file, boolean create) { try {// w w w.j a va 2 s . c o m String path = file.getAbsolutePath(); File parent = file.getParentFile(); if (parent == null) { return DocumentFile.fromFile(file); } DocumentFile f = getDirectory(context, parent, false); if (f == null && create) { f = getDirectory(context, parent, create); } if (f != null) { String name = file.getName(); DocumentFile child = f.findFile(name); if (child != null) { if (child.isFile()) { f = child; } else { f = null; } } else { if (create) { f = f.createFile("application/octet-stream", name); } else { f = null; } } } return f; } catch (Exception e) { LOG.error("Error getting file: {} {}", file, e); return null; } }
From source file:com.frostwire.android.LollipopFileSystem.java
private static DocumentFile getFile(Context context, File file, boolean create) { try {//w w w . j av a 2 s. c o m String path = file.getAbsolutePath(); DocumentFile cached = CACHE.get(path); if (cached != null && cached.isFile()) { return cached; } File parent = file.getParentFile(); if (parent == null) { return DocumentFile.fromFile(file); } DocumentFile f = getDirectory(context, parent, false); if (f == null && create) { f = getDirectory(context, parent, create); } if (f != null) { String name = file.getName(); DocumentFile child = f.findFile(name); if (child != null) { if (child.isFile()) { f = child; } else { f = null; } } else { if (create) { f = f.createFile("application/octet-stream", name); } else { f = null; } } } if (f != null) { CACHE.put(path, f); } return f; } catch (Throwable e) { LOG.error("Error getting file: " + file, e); return null; } }
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++;// w ww . j av a 2 s . co 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.xperia64.timidityae.Globals.java
public static void tryToCreateFile(Context c, String filename) { filename = filename.replace("//", "/"); if (!(new File(filename).exists())) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && 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 . ja v a 2s . com break; } } DocumentFile xx = df; while (i < split.length - 1) { xx = xx.findFile(split[i++]); if (xx == null) { Log.e("TimidityAE Globals", "Create file error."); break; } } if (xx != null) { xx.createFile("timidityae/tpl", split[split.length - 1]); } } else { try { new File(filename).createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } }