TeamNewPipe/NewPipe · error · IOException
Failed to create the tree from Uri
Error message
Failed to create the tree from Uri
What it means
Thrown by the StoredDirectoryHelper constructor for a SAF (Storage Access Framework) tree URI. After successfully calling takePersistableUriPermission, the constructor calls DocumentFile.fromTreeUri(context, path), which returns null when Android cannot resolve the URI to a document tree. The persistable-permission step succeeding does not guarantee fromTreeUri will resolve — a null result means the ContentProvider behind the URI does not expose a tree-document interface or the URI is malformed/expired despite the permission call.
Source
Thrown at app/src/main/java/org/schabi/newpipe/streams/io/StoredDirectoryHelper.java:73
this.tag = tag;
if (ContentResolver.SCHEME_FILE.equalsIgnoreCase(path.getScheme())) {
ioTree = Paths.get(URI.create(path.toString()));
return;
}
this.context = context;
try {
this.context.getContentResolver().takePersistableUriPermission(path, PERMISSION_FLAGS);
} catch (final Exception e) {
throw new IOException(e);
}
this.docTree = DocumentFile.fromTreeUri(context, path);
if (this.docTree == null) {
throw new IOException("Failed to create the tree from Uri");
}
}
public StoredFileHelper createFile(final String filename, final String mime) {
return createFile(filename, mime, false);
}
public StoredFileHelper createUniqueFile(final String name, final String mime) {
final List<String> matches = new ArrayList<>();
final String[] filename = splitFilename(name);
final String lcFileName = filename[0].toLowerCase();
if (docTree == null) {
try (Stream<Path> stream = Files.list(ioTree)) {
matches.addAll(stream.map(path -> path.getFileName().toString().toLowerCase())
.filter(fileName -> fileName.startsWith(lcFileName))
.collect(Collectors.toList()));
} catch (final IOException e) {View on GitHub (pinned to 9e8be09156)
Solutions
- Re-prompt the user to pick the directory via ACTION_OPEN_DOCUMENT_TREE and store the freshly-granted URI.
- Verify the URI scheme is content:// and that it was originally obtained from a tree picker before constructing StoredDirectoryHelper.
- Check that the persistable permission is still held via getContentResolver().getPersistedUriPermissions() before reuse.
- Catch the IOException and fall back to internal/external app-specific storage as a default download location.
Example fix
// before
StoredDirectoryHelper helper = new StoredDirectoryHelper(context, savedUri, tag);
// after — verify the permission and URI are still valid before reuse
List<UriPermission> perms = context.getContentResolver().getPersistedUriPermissions();
boolean stillValid = perms.stream().anyMatch(p -> p.getUri().equals(savedUri));
if (!stillValid) {
savedUri = requestNewTreeUri(); // re-launch ACTION_OPEN_DOCUMENT_TREE
}
StoredDirectoryHelper helper = new StoredDirectoryHelper(context, savedUri, tag); Defensive patterns
Strategy: validation
Validate before calling
// Before constructing StoredDirectoryHelper, confirm the tree URI is still valid:
List<UriPermission> perms = context.getContentResolver().getPersistedUriPermissions();
boolean valid = perms.stream().anyMatch(p -> p.getUri().equals(path) && p.isWritePermission());
if (!valid) {
path = requestNewTree(); // re-launch ACTION_OPEN_DOCUMENT_TREE
} Try / catch
try {
StoredDirectoryHelper helper = new StoredDirectoryHelper(context, uri, tag);
} catch (IOException e) {
// tree URI invalid — prompt user to re-pick
showDirectoryPicker();
} Prevention
- Re-verify persisted URI permissions before reusing a saved tree URI across sessions.
- Always obtain tree URIs from ACTION_OPEN_DOCUMENT_TREE.
- Handle the case where removable storage is unmounted by re-prompting for a directory.
When it happens
Trigger: StoredDirectoryHelper is constructed with a content:// URI that is not a valid tree URI (not obtained from ACTION_OPEN_DOCUMENT_TREE), or a tree URI whose provider has been uninstalled/cleared, or a URI from a removed/reenmounted SD card. DocumentFile.fromTreeUri returns null only in these failure cases.
Common situations: User-selected download folder on a removable SD card that was unmounted; URI persisted across an app reinstall where the persistable permission was actually lost; a URI string saved/restored incorrectly (truncated or from a different user/SAF session); OEM ROM bug where the DocumentsProvider returns null for a valid tree URI.
Related errors
- SAF not available
- Cannot create the file
- Directory with the same name found but cannot delete
- Cannot get the ParcelFileDescriptor for
- Cache dir does not exist >
AI-assisted analysis of TeamNewPipe/NewPipe@9e8be09156 (2026-08-14).
Data as JSON: /api/errors/722b1816c5ff2e07.
Report an issue: GitHub.