android.support.v4.provider.DocumentFile Java Examples
The following examples show how to use
android.support.v4.provider.DocumentFile.
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: SimpleUtils.java From SimpleExplorer with GNU General Public License v3.0 | 6 votes |
public static boolean createDir(File folder) { if (folder.exists()) return false; if (folder.mkdir()) return true; else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { DocumentFile document = DocumentFile.fromFile(folder.getParentFile()); if (document.exists()) return true; } if (Settings.rootAccess()) { return RootCommands.createRootdir(folder); } } return false; }
Example #2
Source File: FileUtil.java From FileManager with Apache License 2.0 | 6 votes |
public static boolean renameTarget(String filePath, String newName) { File src = new File(filePath); String temp = filePath.substring(0, filePath.lastIndexOf("/")); File dest = new File(temp + "/" + newName); if (src.renameTo(dest)) { return true; } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { DocumentFile document = DocumentFile.fromFile(src); if (document.renameTo(dest.getAbsolutePath())) { return true; } } } return false; }
Example #3
Source File: FileUtil.java From FileManager with Apache License 2.0 | 6 votes |
public static boolean createDir(File folder) { if (folder.exists()) return false; if (folder.mkdir()) return true; else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { DocumentFile document = DocumentFile.fromFile(folder.getParentFile()); if (document.exists()) return true; } if (Settings.rootAccess()) { return RootCommands.createRootdir(folder); } } return false; }
Example #4
Source File: TGSafBrowser.java From tuxguitar with GNU Lesser General Public License v2.1 | 6 votes |
public void open(final TGBrowserCallBack<Object> cb){ try { new TGSafBrowserUriRequest(this.context, new TGSafBrowserUriHandler() { public void onUriAccessGranted(Uri uri) { if( uri != null ) { TGSafBrowser.this.root = DocumentFile.fromTreeUri(getActivity(), uri); TGSafBrowser.this.element = null; cb.onSuccess(TGSafBrowser.this.element); } else { cb.handleError(new TGBrowserException(getActivity().getString(R.string.browser_settings_saf_error_uri_access))); } } }, this.data.getUri()).process(); } catch (RuntimeException e) { cb.handleError(e); } }
Example #5
Source File: TGSafBrowser.java From tuxguitar with GNU Lesser General Public License v2.1 | 6 votes |
public void listElements(TGBrowserCallBack<List<TGBrowserElement>> cb) { try { List<TGBrowserElement> elements = new ArrayList<TGBrowserElement>(); if( this.element != null ) { DocumentFile file = this.element.getFile(); if( file.exists() && file.isDirectory() ) { DocumentFile[] children = file.listFiles(); if( children != null ) { for(DocumentFile child : children){ elements.add(new TGSafBrowserElement(child, this.element)); } } } if( !elements.isEmpty() ){ Collections.sort(elements, new TGBrowserElementComparator()); } } cb.onSuccess(elements); } catch (Throwable e) { cb.handleError(e); } }
Example #6
Source File: SettingsImpl.java From ToGoZip with GNU General Public License v3.0 | 6 votes |
/** * calculates the dafault-path value for 2go.zip */ public static String getDefaultZipDirPath(Context context) { File rootDir = null; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { // before api-14/android-4.4/KITKAT // write support on sdcard, if mounted Boolean isSDPresent = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); rootDir = ((isSDPresent)) ? Environment.getExternalStorageDirectory() : Environment.getRootDirectory(); } else if (Global.USE_DOCUMENT_PROVIDER && (zipDocDirUri != null)) { // DocumentFile docDir = DocumentFile.fromTreeUri(context, Uri.parse(zipDocDirUri)); DocumentFile docDir = DocumentFile.fromFile(new File(zipDocDirUri)); if ((docDir != null) && docDir.canWrite()) { return rootDir.getAbsolutePath(); } } if (rootDir == null) { // since android 4.4 Environment.getDataDirectory() and .getDownloadCacheDirectory () // is protected by android-os :-( rootDir = getRootDir44(); } final String zipfile = rootDir.getAbsolutePath() + "/copy"; return zipfile; }
Example #7
Source File: Rename.java From Camera-Roll-Android-App with Apache License 2.0 | 6 votes |
private boolean renameFileRemovableStorage(Context context, Uri treeUri, String path, String newFileName) { //keep old paths to remove them from MediaStore afterwards ArrayList<String> oldPaths = FileOperation.Util.getAllChildPaths(new ArrayList<String>(), path); newFilePath = getNewFilePath(path, newFileName); boolean success = false; DocumentFile file = StorageUtil.parseDocumentFile(context, treeUri, new File(path)); if (file != null) { success = file.renameTo(new File(newFilePath).getName()); } //re-scan all paths ArrayList<String> newPaths = FileOperation.Util.getAllChildPaths(new ArrayList<String>(), newFilePath); addPathsToScan(oldPaths); addPathsToScan(newPaths); return success; }
Example #8
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 #9
Source File: SettingsImpl.java From ToGoZip with GNU General Public License v3.0 | 6 votes |
/** * return true if outputdirectory of zipfile is writable */ public static boolean canWrite(Context context, String dir) { if ((dir == null) || (dir.trim().length() == 0)) { return false; // empty is no valid path } if (Global.USE_DOCUMENT_PROVIDER) { DocumentFile docDir = getDocFile(context, dir); return ((docDir != null) && (docDir.exists()) && docDir.canWrite()); } File fileDir = new File(dir); if ((fileDir == null) || (!fileDir.exists() && !fileDir.mkdirs())) { return false; // parentdir does not exist and cannot be created } return true; // (parentDir.canWrite()); }
Example #10
Source File: SettingsImpl.java From ToGoZip with GNU General Public License v3.0 | 6 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private static DocumentFile getDocFile(Context context, @NonNull String dir ) { DocumentFile docDir = null; if (dir.indexOf(":") >= 0) { Uri uri = Uri.parse(dir); if ("file".equals(uri.getScheme())) { File fileDir = new File(uri.getPath()); docDir = DocumentFile.fromFile(fileDir); } else { docDir = DocumentFile.fromTreeUri(context, uri); } } else { docDir = DocumentFile.fromFile(new File(dir)); } return docDir; }
Example #11
Source File: SettingsActivity.java From ToGoZip with GNU General Public License v3.0 | 6 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private static void grandPermission5(Context ctx, Uri data) { DocumentFile docPath = DocumentFile.fromTreeUri(ctx, data); if (docPath != null) { final ContentResolver resolver = ctx.getContentResolver(); resolver.takePersistableUriPermission(data, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); } }
Example #12
Source File: ZipStorageDocumentFile.java From ToGoZip with GNU General Public License v3.0 | 6 votes |
/** * {@inheritDoc} */ @Override public OutputStream createOutputStream(ZipInstance zipInstance) throws FileNotFoundException { // find existing DocumentFile zipFile = getDocumentFile(zipInstance); // if not found create it. if (zipFile == null) { final String mimetype = (zipInstance == ZipInstance.logfile) ? MIMETYPE_TEXT : MIMETYPE_ZIP; zipFile = directory.createFile(mimetype, getZipFileNameWithoutPath(zipInstance)); } if (zipFile != null) return context.getContentResolver().openOutputStream(zipFile.getUri(), "w"); return null; }
Example #13
Source File: Futils.java From PowerFileExplorer with GNU General Public License v3.0 | 6 votes |
/** * Open file from OTG * @param f * @param c * @param forcechooser */ public void openunknown(DocumentFile f, Context c, boolean forcechooser) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); String type = f.getType(); if (type != null && type.trim().length() != 0 && !type.equals("*/*")) { intent.setDataAndType(f.getUri(), type); Intent startintent; if (forcechooser) startintent = Intent.createChooser(intent, c.getResources().getString(R.string.openwith)); else startintent = intent; try { c.startActivity(startintent); } catch (ActivityNotFoundException e) { e.printStackTrace(); Toast.makeText(c, R.string.noappfound, Toast.LENGTH_SHORT).show(); openWith(f, c); } } else { openWith(f, c); } }
Example #14
Source File: SimpleUtils.java From SimpleExplorer with GNU General Public License v3.0 | 6 votes |
public static boolean renameTarget(String filePath, String newName) { File src = new File(filePath); String temp = filePath.substring(0, filePath.lastIndexOf("/")); File dest = new File(temp + "/" + newName); if (src.renameTo(dest)) { return true; } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { DocumentFile document = DocumentFile.fromFile(src); if (document.renameTo(dest.getAbsolutePath())) { return true; } } } return false; }
Example #15
Source File: HFile.java From PowerFileExplorer with GNU General Public License v3.0 | 6 votes |
/** * Helper method to check file existence in otg * * @param context * @return */ public boolean exists(Context context) { if (isOtgFile()) { DocumentFile fileToCheck = OTGUtil.getDocumentFile(path, context, false); return fileToCheck != null; } else return (exists()); }
Example #16
Source File: TreeUriScannerIntentService.java From fdroidclient with GNU General Public License v3.0 | 6 votes |
/** * Recursively search for {@link IndexV1Updater#SIGNED_FILE_NAME} starting * from the given directory, looking at files first before recursing into * directories. This is "depth last" since the index file is much more * likely to be shallow than deep, and there can be a lot of files to * search through starting at 4 or more levels deep, like the fdroid * icons dirs and the per-app "external storage" dirs. */ private void searchDirectory(DocumentFile documentFileDir) { DocumentFile[] documentFiles = documentFileDir.listFiles(); if (documentFiles == null) { return; } boolean foundIndex = false; ArrayList<DocumentFile> dirs = new ArrayList<>(); for (DocumentFile documentFile : documentFiles) { if (documentFile.isDirectory()) { dirs.add(documentFile); } else if (!foundIndex) { if (IndexV1Updater.SIGNED_FILE_NAME.equals(documentFile.getName())) { registerRepo(documentFile); foundIndex = true; } } } for (DocumentFile dir : dirs) { searchDirectory(dir); } }
Example #17
Source File: FileUtils.java From Rey-MusicPlayer with Apache License 2.0 | 6 votes |
public static boolean deleteFile(File file) { if (file == null) { throw new NullPointerException("File can't be null"); } if (MusicUtils.isFromSdCard(file.getAbsolutePath())) { DocumentFile documentFile = getDocumentFile(file); if (documentFile.isDirectory()) { deleteDirectory(documentFile); } else { Logger.log("Deleted File Name " + file.getName()); return documentFile.delete(); } } else { return file.delete(); } return false; }
Example #18
Source File: FileUtils.java From Rey-MusicPlayer with Apache License 2.0 | 6 votes |
public static void rename(File file, String fileName) { if (MusicUtils.isFromSdCard(file.getAbsolutePath())) { DocumentFile documentFile = getDocumentFile(file); documentFile.renameTo(fileName); } else { File parentFile = file.getParentFile(); if (!parentFile.exists()) parentFile.mkdir(); File file1 = new File(parentFile, fileName); file.renameTo(file1); file.delete(); } }
Example #19
Source File: Copy.java From Camera-Roll-Android-App with Apache License 2.0 | 6 votes |
static boolean copyFileOntoRemovableStorage(Context context, Uri treeUri, String path, String destination) throws IOException { String mimeType = MediaType.getMimeType(path); DocumentFile file = DocumentFile.fromFile(new File(destination)); if (file.exists()) { int index = destination.lastIndexOf("."); destination = destination.substring(0, index) + " Copy" + destination.substring(index, destination.length()); } DocumentFile destinationFile = StorageUtil.createDocumentFile(context, treeUri, destination, mimeType); if (destinationFile != null) { ContentResolver resolver = context.getContentResolver(); OutputStream outputStream = resolver.openOutputStream(destinationFile.getUri()); InputStream inputStream = new FileInputStream(path); return writeStream(inputStream, outputStream); } return false; }
Example #20
Source File: FileOperation.java From Camera-Roll-Android-App with Apache License 2.0 | 6 votes |
public Uri getTreeUri(Intent workIntent, String path) { Log.d("FileOperation", "getTreeUri"); Uri treeUri; String treeUriExtra = workIntent.getStringExtra(FileOperation.REMOVABLE_STORAGE_TREE_URI); if (treeUriExtra != null) { treeUri = Uri.parse(treeUriExtra); } else { Settings s = Settings.getInstance(getApplicationContext()); treeUri = s.getRemovableStorageTreeUri(); } if (path != null) { //check if path is child of the treeUri DocumentFile file = StorageUtil.parseDocumentFile(getApplicationContext(), treeUri, new File(path)); if (file != null) { return treeUri; } else { requestPermissionForRemovableStorageBroadcast(workIntent); } } else { return treeUri; } return null; }
Example #21
Source File: FileUtils.java From Rey-MusicPlayer with Apache License 2.0 | 6 votes |
public static boolean deleteRecursive(File fileOrDirectory) { if (fileOrDirectory.isDirectory()) if (fileOrDirectory.listFiles() != null) for (File child : fileOrDirectory.listFiles()) { return deleteRecursive(child); } if (MusicUtils.isKitkat() || !MusicUtils.isFromSdCard(fileOrDirectory.getAbsolutePath())) { if (fileOrDirectory.delete()) { return MusicUtils.deleteViaContentProvider(fileOrDirectory.getAbsolutePath()); } } else { DocumentFile documentFile = getDocumentFile(fileOrDirectory); if (documentFile.delete()) { return MusicUtils.deleteViaContentProvider(fileOrDirectory.getAbsolutePath()); } } return false; }
Example #22
Source File: StorageUtil.java From Camera-Roll-Android-App with Apache License 2.0 | 5 votes |
public static DocumentFile createDocumentFile(Context context, Uri treeUri, String path, String mimeType) { int index = path.lastIndexOf("/"); String dirPath = path.substring(0, index); DocumentFile file = parseDocumentFile(context, treeUri, new File(dirPath)); if (file != null) { String name = path.substring(index + 1); file = file.createFile(mimeType, name); } return file; }
Example #23
Source File: ShareUtil.java From memetastic with GNU General Public License v3.0 | 5 votes |
/** * Check whether or not a file can be written. * Requires storage access framework permission for external storage (SD) * * @param file The file object (file/folder) * @param isDir Wether or not the given file parameter is a directory * @return Wether or not the file can be written */ public boolean canWriteFile(final File file, final boolean isDir) { if (file == null) { return false; } else if (file.getAbsolutePath().startsWith(Environment.getExternalStorageDirectory().getAbsolutePath()) || file.getAbsolutePath().startsWith(_context.getFilesDir().getAbsolutePath())) { boolean s1 = isDir && file.getParentFile().canWrite(); return !isDir && file.getParentFile() != null ? file.getParentFile().canWrite() : file.canWrite(); } else { DocumentFile dof = getDocumentFile(file, isDir); return dof != null && dof.canWrite(); } }
Example #24
Source File: Delete.java From Camera-Roll-Android-App with Apache License 2.0 | 5 votes |
boolean deleteFileOnRemovableStorage(Context context, Uri treeUri, String path) { boolean success = false; DocumentFile file = StorageUtil.parseDocumentFile(context, treeUri, new File(path)); if (file != null) { success = file.delete(); } //remove from MediaStore addPathToScan(path); return success; }
Example #25
Source File: StorageUtil.java From Camera-Roll-Android-App with Apache License 2.0 | 5 votes |
public static DocumentFile createDocumentDir(Context context, Uri treeUri, String path) { int index = path.lastIndexOf("/"); String dirPath = path.substring(0, index); DocumentFile file = parseDocumentFile(context, treeUri, new File(dirPath)); if (file != null) { String name = path.substring(index + 1); file = file.createDirectory(name); } return file; }
Example #26
Source File: TreeUriScannerIntentService.java From fdroidclient with GNU General Public License v3.0 | 5 votes |
@Override protected void onHandleIntent(Intent intent) { if (intent == null || !ACTION_SCAN_TREE_URI.equals(intent.getAction())) { return; } Uri treeUri = intent.getData(); if (treeUri == null) { return; } Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST); DocumentFile treeFile = DocumentFile.fromTreeUri(this, treeUri); searchDirectory(treeFile); }
Example #27
Source File: ZipStorageDocumentFile.java From ToGoZip with GNU General Public License v3.0 | 5 votes |
/** * {@inheritDoc} */ @Override public boolean rename(ZipInstance zipInstanceFrom, ZipInstance zipInstanceTo) { DocumentFile zipFile = directory.findFile(getZipFileNameWithoutPath(zipInstanceFrom)); if (zipFile != null) return zipFile.renameTo(getZipFileNameWithoutPath(zipInstanceTo)); return false; }
Example #28
Source File: TreeUriDownloader.java From fdroidclient with GNU General Public License v3.0 | 5 votes |
TreeUriDownloader(Uri uri, File destFile) throws FileNotFoundException, MalformedURLException { super(uri, destFile); context = FDroidApp.getInstance(); String path = uri.getEncodedPath(); int lastEscapedSlash = path.lastIndexOf(ESCAPED_SLASH); String pathChunkToEscape = path.substring(lastEscapedSlash + ESCAPED_SLASH.length()); String escapedPathChunk = Uri.encode(pathChunkToEscape); treeUri = uri.buildUpon().encodedPath(path.replace(pathChunkToEscape, escapedPathChunk)).build(); documentFile = DocumentFile.fromTreeUri(context, treeUri); }
Example #29
Source File: ZipStorageDocumentFile.java From ToGoZip with GNU General Public License v3.0 | 5 votes |
/** * {@inheritDoc} */ @Override public InputStream createInputStream() throws FileNotFoundException { DocumentFile zipFile = directory.findFile(filename); if (zipFile != null) return context.getContentResolver().openInputStream(zipFile.getUri()); return null; }
Example #30
Source File: TreeUriScannerIntentService.java From fdroidclient with GNU General Public License v3.0 | 5 votes |
/** * For all files called {@link IndexV1Updater#SIGNED_FILE_NAME} found, check * the JAR signature and read the fingerprint of the signing certificate. * The fingerprint is then used to find whether this local repo is a mirror * of an existing repo, or a totally new repo. In order to verify the * signatures in the JAR, the whole file needs to be read in first. * * @see JarInputStream#JarInputStream(InputStream, boolean) */ private void registerRepo(DocumentFile index) { InputStream inputStream = null; try { inputStream = getContentResolver().openInputStream(index.getUri()); registerRepo(this, inputStream, index.getParentFile().getUri()); } catch (IOException | IndexUpdater.SigningException e) { e.printStackTrace(); } finally { Utils.closeQuietly(inputStream); } }