Java Code Examples for androidx.documentfile.provider.DocumentFile#canWrite()

The following examples show how to use androidx.documentfile.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: DCCManager.java    From revolution-irc with GNU General Public License v3.0 6 votes vote down vote up
private void checkSystemDownloadsDirectoryAccess() {
    if (mDownloadDirectoryOverrideURI != null && !mAlwaysUseFallbackDir) {
        DocumentFile dir = DocumentFile.fromTreeUri(mContext,
                mDownloadDirectoryOverrideURI);
        mHasSystemDirectoryAccess = dir.exists() && dir.canWrite();
        return;
    }

    File downloadsDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS);
    if (downloadsDir != null && downloadsDir.canWrite() && !mAlwaysUseFallbackDir) {
        mDownloadDirectory = downloadsDir;
        mHasSystemDirectoryAccess = true;
    } else {
        mDownloadDirectory = mFallbackDownloadDirectory;
        mHasSystemDirectoryAccess = false;
    }
    Log.d("DCCManager", "Download directory: " +
            (mDownloadDirectory != null ? mDownloadDirectory.getAbsolutePath() : "null"));
}
 
Example 2
Source File: MediaFileUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * キャプチャ用のディレクトリを取得、取得できなければnull
 * こっちはSAF経由でアクセス可能な場合のみ指定場所を示すDocumentFileを返す
 * @param context
 * @param saveTreeId 0: SAFを使わない, それ以外: SAFのツリーIDとみなして処理を試みる
 * @return
 */
@Nullable
public static synchronized DocumentFile getSAFRecordingRoot(
	@NonNull final Context context,
	final int saveTreeId) {

	DocumentFile root = null;
	if (SAFUtils.hasPermission(context, saveTreeId)) {
		try {
			root = SAFUtils.getDir(context, saveTreeId, FileUtils.DIR_NAME);
			if (!root.exists() || !root.canWrite()) {
				Log.d(TAG, "path will be wrong, will already be removed,"
					+ (root != null ? root.getUri() : null));
				root = null;
			}
		} catch (final IOException | IllegalStateException e) {
			root = null;
			Log.d(TAG, "path is wrong, will already be removed.", e);
		}
	}
	if (root == null) {
		if (DEBUG) Log.d(TAG, "getSAFRecordingRoot:保存先を取得できなかったので念のためにセカンダリーストレージアクセスのパーミッションも落としておく");
		SAFUtils.releasePersistableUriPermission(context, saveTreeId);
	}
	return root;
}
 
Example 3
Source File: MimiUtil.java    From mimi-reader with Apache License 2.0 5 votes vote down vote up
public static boolean createSaveDir() {
    try {
        DocumentFile saveDir = getSaveDir();
        if (!saveDir.canWrite()) {
            return false;
        }

        return saveDir.exists();

    } catch (Exception e) {
        Log.e(LOG_TAG, "Error creating dir", e);
    }

    return false;
}
 
Example 4
Source File: SAFUtils.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * 指定したDocumentFileが書き込み可能であればその下にディレクトリを生成して
 * そのディレクトリを示すDocumentFileオブジェクトを返す
 * @param parent
 * @param dirs
 * @return
 * @throws IOException
 */
@NonNull
public static DocumentFile getDir(
	@NonNull final DocumentFile parent, @Nullable final String dirs)
		throws IOException {
	
	DocumentFile tree = parent;
	if (!TextUtils.isEmpty(dirs)) {
		final String[] dir = dirs.split("/");
		for (final String d: dir) {
			if (!TextUtils.isEmpty(d)) {
				final DocumentFile t = tree.findFile(d);
				if ((t != null) && t.isDirectory()) {
					// 既に存在している時は何もしない
					tree = t;
				} else if (t == null) {
					if (tree.canWrite()) {
						// 存在しないときはディレクトリを生成
						tree = tree.createDirectory(d);
					} else {
						throw new IOException("can't create directory");
					}
				} else {
					throw new IOException("can't create directory, file with same name already exists");
				}
			}
		}
	}
	return tree;
}
 
Example 5
Source File: SAFUtils.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * 指定したDocumentFileが指し示すフォルダの下に指定した相対パスのディレクトリ階層を生成する
 * フォルダが存在していない時に書き込み可能でなければIOExceptionを投げる
 * deprecated #getDirを使うこと
 * @param context
 * @param baseDoc
 * @param dirs
 * @return
 * @throws IOException
 */
@Deprecated
@NonNull
public static DocumentFile getDocumentFile(
	@NonNull Context context,
	@NonNull final DocumentFile baseDoc, @Nullable final String dirs)
		throws IOException {
	
	DocumentFile tree = baseDoc;
	if (!TextUtils.isEmpty(dirs)) {
		final String[] dir = dirs.split("/");
		for (final String d: dir) {
			if (!TextUtils.isEmpty(d)) {
				final DocumentFile t = tree.findFile(d);
				if ((t != null) && t.isDirectory()) {
					// 既に存在している時は何もしない
					tree = t;
				} else if (t == null) {
					if (tree.canWrite()) {
						// 存在しないときはディレクトリを生成
						tree = tree.createDirectory(d);
					} else {
						throw new IOException("can't create directory");
					}
				} else {
					throw new IOException("can't create directory, file with same name already exists");
				}
			}
		}
	}
	return tree;
}
 
Example 6
Source File: SAFUtils.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * 指定したDocumentFileが書き込み可能であればその下にディレクトリを生成して
 * そのディレクトリを示すDocumentFileオブジェクトを返す
 * @param parent
 * @param dirs
 * @return
 * @throws IOException
 */
@NonNull
public static DocumentFile getDir(
	@NonNull final DocumentFile parent, @Nullable final String dirs)
		throws IOException {
	
	DocumentFile tree = parent;
	if (!TextUtils.isEmpty(dirs)) {
		final String[] dir = dirs.split("/");
		for (final String d: dir) {
			if (!TextUtils.isEmpty(d)) {
				if ("..".equals(d)) {
					tree = tree.getParentFile();
					if (tree == null) {
						throw new IOException("failed to get parent directory");
					}
				} else if (!".".equals(d)) {
					final DocumentFile t = tree.findFile(d);
					if ((t != null) && t.isDirectory()) {
						// 既に存在している時は何もしない
						tree = t;
					} else if (t == null) {
						if (tree.canWrite()) {
							// 存在しないときはディレクトリを生成
							tree = tree.createDirectory(d);
						} else {
							throw new IOException("can't create directory");
						}
					} else {
						throw new IOException("can't create directory, file with same name already exists");
					}
				}
			}
		}
	}
	return tree;
}
 
Example 7
Source File: BaseUriFtpFile.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isWritable() {
    if (mDocument.exists())
        return mDocument.canWrite();
    DocumentFile parent = mDocument.getParentFile();
    while (parent != null) {
        if (parent.exists())
            return parent.canWrite();
        parent = parent.getParentFile();
    }
    return false;
}
 
Example 8
Source File: LocalBackupStorageProvider.java    From SAI with GNU General Public License v3.0 4 votes vote down vote up
private boolean isUriValid(Uri uri) {
    DocumentFile docFile = SafUtils.docFileFromTreeUriOrFileUri(mContext, uri);
    return docFile != null && docFile.canRead() && docFile.canWrite();
}