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

The following examples show how to use androidx.documentfile.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: SAFUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定したUriが存在する時にその下に出力用ファイルを生成してOutputStreamとして返す
 * @param context
 * @param parent
 * @param dirs
 * @param mime
 * @param name
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 */
@NonNull
public static InputStream getInputStream(
	@NonNull final Context context,
	@NonNull final DocumentFile parent, @Nullable final String dirs,
	final String mime, final String name) throws IOException {

	final DocumentFile tree = getDir(parent, dirs);
	final DocumentFile file = tree.findFile(name);
	if (file != null) {
		if (file.isFile()) {
			return context.getContentResolver().openInputStream(
				file.getUri());
		} else {
			throw new IOException("directory with same name already exists");
		}
	} else {
		throw new FileNotFoundException("specific file not found");
	}
}
 
Example 2
Source File: SAFUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定したUriが存在する時にその下に出力用ファイルを生成してOutputStreamとして返す
 * @param context
 * @param parent
 * @param dirs
 * @param mime
 * @param name
 * @return
 * @throws FileNotFoundException
 */
@NonNull
public static OutputStream getOutputStream(
	@NonNull final Context context,
	@NonNull final DocumentFile parent, @Nullable final String dirs,
	final String mime, final String name) throws IOException {

	final DocumentFile tree = getDir(parent, dirs);
	final DocumentFile file = tree.findFile(name);
	if (file != null) {
		if (file.isFile()) {
			return context.getContentResolver().openOutputStream(
				file.getUri());
		} else {
			throw new IOException("directory with same name already exists");
		}
	} else {
		return context.getContentResolver().openOutputStream(
			tree.createFile(mime, name).getUri());
	}
}
 
Example 3
Source File: DocumentUtil.java    From HaoReader with GNU General Public License v3.0 6 votes vote down vote up
public static DocumentFile createDirIfNotExist(DocumentFile root, String... subDirs) {
    DocumentFile parent = root;
    try {
        for (int i = 0; i < subDirs.length; i++) {
            String subDirName = filenameFilter(Uri.decode(subDirs[i]));
            DocumentFile subDir = parent.findFile(subDirName);
            if (subDir == null) {
                subDir = parent.createDirectory(subDirName);
            }
            parent = subDir;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return parent;
}
 
Example 4
Source File: DocumentUtil.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static DocumentFile getDirDocument(DocumentFile root, String... subDirs) {
    DocumentFile parent = root;
    for (int i = 0; i < subDirs.length; i++) {
        String subDirName = Uri.decode(subDirs[i]);
        DocumentFile subDir = parent.findFile(subDirName);
        if (subDir != null)
            parent = subDir;
        else
            return null;
    }
    return parent;
}
 
Example 5
Source File: DocumentUtil.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isFileExist(String fileName, DocumentFile root, String... subDirs) {
    DocumentFile parent = getDirDocument(root, subDirs);
    if (parent == null)
        return false;
    fileName = filenameFilter(Uri.decode(fileName));
    DocumentFile file = parent.findFile(fileName);
    if (file != null && file.exists())
        return true;
    return false;
}
 
Example 6
Source File: DocumentUtil.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
public static boolean writeBytes(Context context, byte[] data, String fileName, String rootPath, String... subDirs) {
    DocumentFile parent = getDirDocument(context, rootPath, subDirs);
    if (parent == null)
        return false;
    DocumentFile file = parent.findFile(fileName);
    return writeBytes(context, data, file.getUri());
}
 
Example 7
Source File: DocumentUtil.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static InputStream getFileInputSteam(Context context, String fileName, DocumentFile root, String... subDirs) {
    DocumentFile parent = getDirDocument(root, subDirs);
    if (parent == null)
        return null;
    DocumentFile file = parent.findFile(fileName);
    if (file == null)
        return null;
    return getFileInputSteam(context, file.getUri());
}
 
Example 8
Source File: DocumentUtil.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static InputStream getFileInputSteam(Context context, String fileName, Uri rootUri, String... subDirs) {
    DocumentFile parent = getDirDocument(context, rootUri, subDirs);
    if (parent == null)
        return null;
    fileName = filenameFilter(Uri.decode(fileName));
    DocumentFile file = parent.findFile(fileName);
    if (file == null)
        return null;
    return getFileInputSteam(context, file.getUri());
}
 
Example 9
Source File: DocumentUtil.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static InputStream getFileInputSteam(Context context, String fileName, String rootPath, String... subDirs) {
    DocumentFile parent = getDirDocument(context, rootPath, subDirs);
    if (parent == null)
        return null;
    DocumentFile file = parent.findFile(fileName);
    if (file == null)
        return null;
    return getFileInputSteam(context, file.getUri());
}
 
Example 10
Source File: DocumentUtil.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static OutputStream getFileOutputSteam(Context context, String fileName, DocumentFile root, String... subDirs) {
    DocumentFile parent = getDirDocument(root, subDirs);
    if (parent == null)
        return null;
    DocumentFile file = parent.findFile(fileName);
    if (file == null)
        return null;
    return getFileOutputSteam(context, file.getUri());
}
 
Example 11
Source File: DocumentUtil.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
public static boolean writeBytes(Context context, byte[] data, String fileName, Uri rootUri, String... subDirs) {
    DocumentFile parent = getDirDocument(context, rootUri, subDirs);
    if (parent == null)
        return false;
    fileName = filenameFilter(Uri.decode(fileName));
    DocumentFile file = parent.findFile(fileName);
    return writeBytes(context, data, file.getUri());
}
 
Example 12
Source File: DocumentUtil.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static OutputStream getFileOutputSteam(Context context, String fileName, String rootPath, String... subDirs) {
    DocumentFile parent = getDirDocument(context, rootPath, subDirs);
    if (parent == null)
        return null;
    DocumentFile file = parent.findFile(fileName);
    if (file == null)
        return null;
    return getFileOutputSteam(context, file.getUri());
}
 
Example 13
Source File: DocumentUtil.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isFileExist(String fileName, DocumentFile root, String... subDirs) {
    DocumentFile parent = getDirDocument(root, subDirs);
    if (parent == null)
        return false;
    fileName = filenameFilter(Uri.decode(fileName));
    DocumentFile file = parent.findFile(fileName);
    if (file != null && file.exists())
        return true;
    return false;
}
 
Example 14
Source File: DocumentUtil.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] readBytes(Context context, String fileName, DocumentFile root, String... subDirs) {
    DocumentFile parent = getDirDocument(root, subDirs);
    if (parent == null)
        return null;
    fileName = filenameFilter(Uri.decode(fileName));
    DocumentFile file = parent.findFile(fileName);
    if (file == null)
        return null;
    return readBytes(context, file.getUri());
}
 
Example 15
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 16
Source File: SAFUtils.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * 指定したUriが存在する時にその下に入力用ファイルを生成して入出力用のファイルディスクリプタを返す
 * @param context
 * @param treeId
 * @param dirs
 * @param mime
 * @param name
 * @return
 * @throws UnsupportedOperationException
 * @throws FileNotFoundException
 * @throws IOException
 */
@NonNull
public static ParcelFileDescriptor getFd(
	@NonNull final Context context,
	final int treeId, @Nullable final String dirs,
	final String mime, final String name) throws IOException {

	if (BuildCheck.isLollipop()) {
		final DocumentFile tree = getDir(context, treeId, dirs);
		if (tree != null) {
			final DocumentFile file = tree.findFile(name);
			if (file != null) {
				if (file.isFile()) {
					return context.getContentResolver().openFileDescriptor(
						file.getUri(), "rw");
				} else {
					throw new IOException("directory with same name already exists");
				}
			} else {
				return context.getContentResolver().openFileDescriptor(
					tree.createFile(mime, name).getUri(), "rw");
			}
		} else {
			throw new FileNotFoundException("specific dir not found");
		}
	} else {
		throw new UnsupportedOperationException("should be API>=21");
	}
}
 
Example 17
Source File: DocumentUtil.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static boolean writeBytes(Context context, byte[] data, String fileName, Uri rootUri, String... subDirs) {
    DocumentFile parent = getDirDocument(context, rootUri, subDirs);
    if (parent == null)
        return false;
    fileName = filenameFilter(Uri.decode(fileName));
    DocumentFile file = parent.findFile(fileName);
    return writeBytes(context, data, file.getUri());
}
 
Example 18
Source File: DocumentUtil.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] readBytes(Context context, String fileName, DocumentFile root, String... subDirs) {
    DocumentFile parent = getDirDocument(root, subDirs);
    if (parent == null)
        return null;
    fileName = filenameFilter(Uri.decode(fileName));
    DocumentFile file = parent.findFile(fileName);
    if (file == null)
        return null;
    return readBytes(context, file.getUri());
}
 
Example 19
Source File: DocumentUtil.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
public static boolean writeBytes(Context context, byte[] data, String fileName, Uri rootUri, String... subDirs) {
    DocumentFile parent = getDirDocument(context, rootUri, subDirs);
    if (parent == null)
        return false;
    fileName = filenameFilter(Uri.decode(fileName));
    DocumentFile file = parent.findFile(fileName);
    return writeBytes(context, data, file.getUri());
}
 
Example 20
Source File: DocumentUtil.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
public static InputStream getFileInputSteam(Context context, String fileName, Uri rootUri, String... subDirs) {
    DocumentFile parent = getDirDocument(context, rootUri, subDirs);
    if (parent == null)
        return null;
    fileName = filenameFilter(Uri.decode(fileName));
    DocumentFile file = parent.findFile(fileName);
    if (file == null)
        return null;
    return getFileInputSteam(context, file.getUri());
}