Java Code Examples for android.support.v4.provider.DocumentFile#canWrite()

The following examples show how to use android.support.v4.provider.DocumentFile#canWrite() . 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: SettingsImpl.java    From ToGoZip with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 2
Source File: SettingsImpl.java    From ToGoZip with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 3
Source File: RootHelper.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
public static String parseDocumentFilePermission(DocumentFile file) {
    String per = "";
    if (file.canRead()) {
        per = per + "r";
    }
    if (file.canWrite()) {
        per = per + "w";
    }
    if (file.canWrite()) {
        per = per + "x";
    }
    return per;
}
 
Example 4
Source File: ShareUtil.java    From memetastic with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 5
Source File: ShareUtil.java    From openlauncher with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    }
}