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

The following examples show how to use androidx.documentfile.provider.DocumentFile#listFiles() . 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: LocalBackupStorage.java    From SAI with GNU General Public License v3.0 6 votes vote down vote up
@Override
public List<Uri> listBackupFiles() {
    List<Uri> uris = new ArrayList<>();

    DocumentFile backupsDir = SafUtils.docFileFromTreeUriOrFileUri(mContext, getBackupDirUriOrThrow());
    if (backupsDir == null)
        return uris;

    for (DocumentFile docFile : backupsDir.listFiles()) {
        if (docFile.isDirectory())
            continue;

        String docName = docFile.getName();
        if (docName == null)
            continue;

        String docExt = Utils.getExtension(docName);
        if (docExt == null || !docExt.toLowerCase().equals("apks"))
            continue;

        uris.add(namespaceUri(docFile.getUri()));
    }

    return uris;
}
 
Example 2
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 3
Source File: SAFUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定したディレクトリ配下に存在するファイルの一覧を取得
 * @param dir
 * @param filter nullなら存在するファイルを全て追加
 * @return
 */
@NonNull
public static List<DocumentFile> listFiles(
	@NonNull final DocumentFile dir,
	@Nullable final FileFilter filter) {

	final List<DocumentFile> result = new ArrayList<DocumentFile>();
	if (dir.isDirectory()) {
		final DocumentFile[] files = dir.listFiles();
		for (final DocumentFile file: files) {
			if ((filter == null) || (filter.accept(file))) {
				result.add(file);
			}
		}
	}
	return result;
}
 
Example 4
Source File: SAFUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定したディレクトリ配下に存在するファイルの一覧を取得
 * @param dir
 * @param filter nullなら存在するファイルを全て追加
 * @return
 */
@NonNull
public static List<DocumentFile> listFiles(
	@NonNull final DocumentFile dir,
	@Nullable final FileFilter filter) {

	final List<DocumentFile> result = new ArrayList<DocumentFile>();
	if (dir.isDirectory()) {
		final DocumentFile[] files = dir.listFiles();
		for (final DocumentFile file: files) {
			if ((filter == null) || (filter.accept(file))) {
				result.add(file);
			}
		}
	}
	return result;
}