List of usage examples for android.support.v4.provider DocumentFile canWrite
public abstract boolean canWrite();
From source file:Main.java
@NonNull public static boolean dirExistsAndIsWritable(DocumentFile appdir) { return appdir.exists() && appdir.canWrite(); }
From source file:com.frostwire.android.StoragePicker.java
public static String handle(Context context, int requestCode, int resultCode, Intent data) { String result = null;//from www.j a v a2 s. c o 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:de.k3b.android.toGoZip.SettingsImpl.java
/** * return true if outputdirectory of zipfile is writable *//* www . j av a 2 s. c o m*/ 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.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()); }
From source file:org.dkf.jmule.StoragePicker.java
public static String handle(Context context, int requestCode, int resultCode, Intent data) { String result = null;// w w w. j a v a 2s. c om try { if (resultCode == Activity.RESULT_OK && requestCode == SELECT_FOLDER_REQUEST_CODE) { Uri treeUri = data.getData(); ContentResolver cr = context.getContentResolver(); Method takePersistableUriPermissionM = cr.getClass().getMethod("takePersistableUriPermission", Uri.class, int.class); final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); takePersistableUriPermissionM.invoke(cr, 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 { if (Platforms.get().saf()) { LollipopFileSystem fs = (LollipopFileSystem) Platforms.fileSystem(); result = fs.getTreePath(treeUri); // TODO - remove below code - only for testing SD card writing File testFile = new File(result, "test_file.txt"); LOG.info("test file {}", testFile); try { Pair<ParcelFileDescriptor, DocumentFile> fd = fs.openFD(testFile, "rw"); if (fd != null && fd.first != null && fd.second != null) { AndroidFileHandler ah = new AndroidFileHandler(testFile, fd.second, fd.first); ByteBuffer bb = ByteBuffer.allocate(48); bb.putInt(1).putInt(2).putInt(3).putInt(44).putInt(22); bb.flip(); ah.getWriteChannel().write(bb); ah.close(); } else { LOG.error("unable to create file {}", testFile); } } catch (Exception e) { LOG.error("unable to fill file {} error {}", testFile, e); } } } } } } catch (Exception e) { UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_error); LOG.error("Error handling folder selection {}", e); result = null; } return result; }
From source file:de.k3b.android.toGoZip.SettingsImpl.java
/** * calculates the dafault-path value for 2go.zip *//*w w w. ja va 2s.c om*/ 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; }
From source file:com.amaze.carbonfilemanager.filesystem.RootHelper.java
public static String parseDocumentFilePermission(DocumentFile file) { String per = ""; if (file.canRead()) { per = per + "r"; }/* w ww . ja v a 2s.c o m*/ if (file.canWrite()) { per = per + "w"; } if (file.canWrite()) { per = per + "x"; } return per; }
From source file:com.frostwire.android.gui.StoragePicker.java
public static String handle(Context context, int requestCode, int resultCode, Intent data) { String result = null;//ww w.ja v a 2s. c o m try { if (resultCode == Activity.RESULT_OK && requestCode == SELECT_FOLDER_REQUEST_CODE) { Uri treeUri = data.getData(); ContentResolver cr = context.getContentResolver(); Method takePersistableUriPermissionM = cr.getClass().getMethod("takePersistableUriPermission", Uri.class, int.class); final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); takePersistableUriPermissionM.invoke(cr, 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 { result = getFullPathFromTreeUri(context, treeUri); } } } } catch (Throwable e) { UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_error); LOG.error("Error handling folder selection", e); result = null; } return result; }
From source file:com.almalence.googsharing.Thumbnail.java
public static String getName() { String name = ""; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { DocumentFile saveDir = PluginManagerBase.getSaveDirNew(false); if (saveDir != null) { if (!saveDir.canWrite()) { saveDir = PluginManagerBase.getSaveDirNew(true); }/*from w w w . j a v a2 s.co m*/ // Try to build max deep file path // DocumentFile parentFile = saveDir.getParentFile(); // name = saveDir.getName(); // while (parentFile != null) // { // name = parentFile.getName() + "/" + name; // parentFile = parentFile.getParentFile(); // } // // // If we able to get File object, than get path from it // try // { // File file = new File(URI.create(saveDir.getUri().toString())); // name = file.getAbsolutePath(); // } catch (Exception e) // { // } finally // { // } // If we able to get File object, than get path from it. // fileObject should not be null for files on phone memory. File fileObject = Util.getFileFromDocumentFile(saveDir); if (fileObject != null) { name = fileObject.getAbsolutePath(); } else { // This case should typically happen for files saved to SD // card. name = Util.getAbsolutePathFromDocumentFile(saveDir); } } } else { File saveDir = PluginManagerBase.getSaveDir(false); if (!saveDir.canWrite()) { saveDir = PluginManagerBase.getSaveDir(true); } name = saveDir.getAbsolutePath(); } return name; }
From source file:freed.cam.ui.themesample.settings.childs.SettingsChildMenuSDSave.java
@Override public void onActivityResultCallback(Uri uri) { DocumentFile f = DocumentFile.fromTreeUri(getContext(), uri); if (f.canWrite() && lastval.equals(SDModeParameter.external)) { fragment_activityInterface.getAppSettings().SetWriteExternal(true); onParameterValueChanged(SDModeParameter.external); } else {//from w w w . ja va 2s. c o m fragment_activityInterface.getAppSettings().SetWriteExternal(false); onParameterValueChanged(SDModeParameter.internal); } lastval = ""; }
From source file:com.filemanager.free.filesystem.FileUtil.java
/** * Check for a directory if it is possible to create files within this directory, either via normal writing or via * Storage Access Framework./* w w w .ja v a 2s . com*/ * * @param folder The directory * @return true if it is possible to write in this directory. */ public static final boolean isWritableNormalOrSaf(final File folder, Context c) { // Verify that this is a directory. if (folder == null) return false; if (!folder.exists() || !folder.isDirectory()) { return false; } // Find a non-existing file in this directory. int i = 0; File file; do { String fileName = "AugendiagnoseDummyFile" + (++i); file = new File(folder, fileName); } while (file.exists()); // First check regular writability if (isWritable(file)) { return true; } // Next check SAF writability. DocumentFile document = getDocumentFile(file, false, c); if (document == null) { return false; } // This should have created the file - otherwise something is wrong with access URL. boolean result = document.canWrite() && file.exists(); // Ensure that the dummy file is not remaining. deleteFile(file, c); return result; }