Java Code Examples for android.support.v4.provider.DocumentFile#findFile()
The following examples show how to use
android.support.v4.provider.DocumentFile#findFile() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocumentFileUtil.java From apkextractor with GNU General Public License v3.0 | 6 votes |
/** * 通过segment片段定位到parent的指定文件夹,如果没有则尝试创建 */ public static @NonNull DocumentFile getDocumentFileBySegments(@NonNull DocumentFile parent, @Nullable String segment) throws Exception{ if(segment==null)return parent; String[]segments=segment.split("/"); DocumentFile documentFile=parent; for(int i=0;i<segments.length;i++){ DocumentFile lookup=documentFile.findFile(segments[i]); if(lookup==null){ lookup=documentFile.createDirectory(segments[i]); } if(lookup==null){ throw new Exception("Can not create folder "+segments[i]); } documentFile=lookup; } return documentFile; }
Example 2
Source File: OTGUtil.java From PowerFileExplorer with GNU General Public License v3.0 | 5 votes |
/** * Traverse to a specified path in OTG * * @param path * @param context * @param createRecursive flag used to determine whether to create new file while traversing to path, * in case path is not present. Notably useful in opening an output stream. * @return */ public static DocumentFile getDocumentFile(String path, Context context, boolean createRecursive) { SharedPreferences manager = PreferenceManager.getDefaultSharedPreferences(context); String rootUriString = manager.getString(ExplorerActivity.KEY_PREF_OTG, null); // start with root of SD card and then parse through document tree. DocumentFile rootUri = DocumentFile.fromTreeUri(context, Uri.parse(rootUriString)); String[] parts = path.split("/"); for (int i = 0; i < parts.length; i++) { if (path.equals("otg:/")) break; if (parts[i].equals("otg:") || parts[i].equals("")) continue; Log.d(context.getClass().getSimpleName(), "Currently at: " + parts[i]); // iterating through the required path to find the end point DocumentFile nextDocument = rootUri.findFile(parts[i]); if (createRecursive) { if (nextDocument == null || !nextDocument.exists()) { nextDocument = rootUri.createFile(parts[i].substring(parts[i].lastIndexOf(".")), parts[i]); Log.d(context.getClass().getSimpleName(), "NOT FOUND! File created: " + parts[i]); } } rootUri = nextDocument; } return rootUri; }
Example 3
Source File: OutputUtil.java From apkextractor with GNU General Public License v3.0 | 5 votes |
public static @Nullable DocumentFile getWritingDocumentFileForAppItem(@NonNull Context context,@NonNull AppItem appItem,@NonNull String extension,int sequence_number) throws Exception{ String writingFileName=getWriteFileNameForAppItem(context,appItem,extension,sequence_number); DocumentFile parent=getExportPathDocumentFile(context); DocumentFile documentFile=parent.findFile(writingFileName); if(documentFile!=null&&documentFile.exists())documentFile.delete(); return parent.createFile(extension.toLowerCase().equals("apk")?"application/vnd.android.package-archive":"application/x-zip-compressed",writingFileName); }
Example 4
Source File: FileUtils.java From Rey-MusicPlayer with Apache License 2.0 | 5 votes |
@TargetApi(Build.VERSION_CODES.KITKAT) public static DocumentFile getDocumentFile(final File file) { String baseFolder = getExtSdCardFolder(file); String relativePath = null; if (baseFolder == null) { return null; } try { String fullPath = file.getCanonicalPath(); relativePath = fullPath.substring(baseFolder.length() + 1); } catch (IOException e) { Logger.log(e.getMessage()); return null; } Uri treeUri = Common.getInstance().getContentResolver().getPersistedUriPermissions().get(0).getUri(); if (treeUri == null) { return null; } // start with root of SD card and then parse through document tree. DocumentFile document = DocumentFile.fromTreeUri(Common.getInstance(), treeUri); String[] parts = relativePath.split("\\/"); for (String part : parts) { DocumentFile nextDocument = document.findFile(part); if (nextDocument != null) { document = nextDocument; } } return document; }
Example 5
Source File: FileUtil.java From PowerFileExplorer with GNU General Public License v3.0 | 4 votes |
/** * 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) { final String baseFolder = getExtSdCardFolder(file, context); boolean originalDirectory = false; if (baseFolder == null) { return null; } String relativePath = null; try { final 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 } final 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; final String[] parts = relativePath.split("\\/"); //Log.d("FileUtil", "relativePath " + relativePath); for (int i = 0; i < parts.length; i++) { //Log.d("FileUtil", "parts[] " + parts[i]); if (document == null) { return null; } 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]); } } //Log.d("FileUtil", "nextDocument " + nextDocument); document = nextDocument; } return document; }
Example 6
Source File: OTGUtil.java From PowerFileExplorer with GNU General Public License v3.0 | 4 votes |
/** * Returns an array of list of files at a specific path in OTG * * @param path the path to the directory tree, starts with prefix 'otg:/' * Independent of URI (or mount point) for the OTG * @param context context for loading * @return an array of list of files at the path */ public static ArrayList<BaseFile> getDocumentFilesList(final String path, final Context context, final ContentFragment.LoadFiles updater) { final SharedPreferences manager = PreferenceManager.getDefaultSharedPreferences(context); final String rootUriString = manager.getString(ExplorerActivity.KEY_PREF_OTG, null); DocumentFile rootUri = DocumentFile.fromTreeUri(context, Uri.parse(rootUriString)); ArrayList<BaseFile> files = new ArrayList<>(1024); final String[] parts = path.split("/"); for (int i = 0; i < parts.length; i++) { // first omit 'otg:/' before iterating through DocumentFile if (path.equals(OTGUtil.PREFIX_OTG + "/")) break; if (parts[i].equals("otg:") || parts[i].equals("")) continue; Log.d(context.getClass().getSimpleName(), "Currently at: " + parts[i]); // iterating through the required path to find the end point rootUri = rootUri.findFile(parts[i]); } long prevUpdate = System.currentTimeMillis(); Log.d(context.getClass().getSimpleName(), "Found URI for: " + rootUri.getName()); // we have the end point DocumentFile, list the files inside it and return for (DocumentFile file : rootUri.listFiles()) { try { if (file.exists()) { long size = 0; if (!file.isDirectory()) size = file.length(); Log.d(context.getClass().getSimpleName(), "Found file: " + file.getName()); BaseFile baseFile = new BaseFile(path + "/" + file.getName(), RootHelper.parseDocumentFilePermission(file), file.lastModified(), size, file.isDirectory()); baseFile.setName(file.getName()); baseFile.setMode(OpenMode.OTG); files.add(baseFile); final long present = System.currentTimeMillis(); if (updater != null && present - prevUpdate > 1000 && !updater.busyNoti) { prevUpdate = present; updater.publish(files); files = new ArrayList<>(1024); } } } catch (Exception e) { } } if (updater != null) { updater.publish(files); } return files; }
Example 7
Source File: AndroidPathUtils.java From PowerFileExplorer with GNU General Public License v3.0 | 4 votes |
/** * 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. * @param createDirectories flag indicating if intermediate path directories should be created if not existing. * @return The DocumentFile */ public static DocumentFile getDocumentFile(@NonNull final File file, final boolean isDirectory, final boolean createDirectories, final Context c) { // Log.d("getDocumentFile start", df.format(System.currentTimeMillis())); String baseFolder = null; baseFolder = getExtSdCardFolder(file, c); if (baseFolder == null) { return null; } String relativePath; try { String fullPath = file.getCanonicalPath(); relativePath = fullPath.substring(baseFolder.length() + 1); } catch (IOException e) { return null; } if (treeUri == null) { treeUri = getSharedPreferenceUri("key_internal_uri_extsdcard", c); if (treeUri == null) { return null; } } // start with root of SD card and then parse through document tree. DocumentFile document = DocumentFile.fromTreeUri(c, treeUri); 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) { if (createDirectories) { nextDocument = document.createDirectory(parts[i]); } else { return null; } } else if (isDirectory) { nextDocument = document.createDirectory(parts[i]); } else { nextDocument = document.createFile("image", parts[i]); } } document = nextDocument; } // Log.d("getDocumentFile end", df.format(System.currentTimeMillis())); return document; }
Example 8
Source File: DeleteFileUtils.java From Gallery-example with GNU General Public License v3.0 | 2 votes |
private static void deleteFileSAF(String url, final Activity activity) { if (SDCardUtils.getSDCardUri(activity).isEmpty()) { PermissionUtils.askSDCardAccess(activity); } else { File file = new File(url); DocumentFile documentFile = DocumentFile.fromTreeUri(activity, Uri.parse(SDCardUtils.getSDCardUri(activity))); String[] parts = (file.getPath()).split("\\/"); for (int i = 3; i < parts.length; i++) { if (documentFile != null) { documentFile = documentFile.findFile(parts[i]); } } if (documentFile == null) { activity.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(activity, activity.getString(R.string.notFound), Toast.LENGTH_SHORT) .show(); Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); rationaleDialog(activity, activity.getString(R.string.sdcard), activity.getString(R.string.sdcardContent), 2, intent); } }); } else { if (documentFile.delete()) { deleteFileFromMediaStore(activity.getContentResolver(), file); } } } }