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

The following examples show how to use androidx.documentfile.provider.DocumentFile#isFile() . 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: SAFUtil.java    From VinylMusicPlayer with GNU General Public License v3.0 6 votes vote down vote up
/**
 * https://github.com/vanilla-music/vanilla-music-tag-editor/commit/e00e87fef289f463b6682674aa54be834179ccf0#diff-d436417358d5dfbb06846746d43c47a5R359
 * Finds needed file through Document API for SAF. It's not optimized yet - you can still gain wrong URI on
 * files such as "/a/b/c.mp3" and "/b/a/c.mp3", but I consider it complete enough to be usable.
 *
 * @param dir      - document file representing current dir of search
 * @param segments - path segments that are left to find
 * @return URI for found file. Null if nothing found.
 */
@Nullable
public static Uri findDocument(DocumentFile dir, List<String> segments) {
    for (DocumentFile file : dir.listFiles()) {
        int index = segments.indexOf(file.getName());
        if (index == -1) {
            continue;
        }

        if (file.isDirectory()) {
            segments.remove(file.getName());
            return findDocument(file, segments);
        }

        if (file.isFile() && index == segments.size() - 1) {
            // got to the last part
            return file.getUri();
        }
    }

    return null;
}
 
Example 2
Source File: SAFUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定したDocumentFileの下にファイルを生成する
 * dirsがnullまたは空文字列ならDocumentFile#createFileを呼ぶのと同じ
 * @param parent
 * @param dirs
 * @param mime
 * @param name
 * @return
 * @throws IOException
 */
@NonNull
public static DocumentFile getFile(
	@NonNull final DocumentFile parent,
	@Nullable final String dirs,
	@NonNull final String mime,
	@NonNull final String name) throws IOException {
	
	final DocumentFile tree = getDir(parent, dirs);
	final DocumentFile file = tree.findFile(name);
	if (file != null) {
		if (file.isFile()) {
			return file;
		} else {
			throw new IOException("directory with same name already exists");
		}
	} else {
		return tree.createFile(mime, name);
	}
}
 
Example 3
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 4
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 5
Source File: SAFUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定したDocumentFileの示すディレクトリが存在していれば入出力用のファイルディスクリプタを返す
 * @param context
 * @param parent
 * @param dirs
 * @param mime
 * @param name
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 */
@NonNull
public static ParcelFileDescriptor getFd(
	@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().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");
	}
}
 
Example 6
Source File: SAFUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定したDocumentFileの下にファイルを生成する
 * dirsがnullまたは空文字列ならDocumentFile#createFileを呼ぶのと同じ
 * @param parent
 * @param dirs
 * @param mime
 * @param name
 * @return
 * @throws IOException
 */
@NonNull
public static DocumentFile getFile(
	@NonNull final DocumentFile parent,
	@Nullable final String dirs,
	@NonNull final String mime,
	@NonNull final String name) throws IOException {
	
	final DocumentFile tree = getDir(parent, dirs);
	final DocumentFile file = tree.findFile(name);
	if (file != null) {
		if (file.isFile()) {
			return file;
		} else {
			throw new IOException("directory with same name already exists");
		}
	} else {
		return tree.createFile(mime, name);
	}
}
 
Example 7
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 8
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 9
Source File: SAFUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定したDocumentFileの示すディレクトリが存在していれば入出力用のファイルディスクリプタを返す
 * @param context
 * @param parent
 * @param dirs
 * @param mime
 * @param name
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 */
@NonNull
public static ParcelFileDescriptor getFd(
	@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().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");
	}
}
 
Example 10
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 11
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");
	}
}