{"record":{"id":"a3b678d7dcce227f","repo":"TeamNewPipe/NewPipe","slug":"cannot-create-the-file","errorCode":null,"errorMessage":"Cannot create the file","messagePattern":"Cannot create the file","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/schabi/newpipe/streams/io/StoredFileHelper.java","lineNumber":92,"sourceCode":"            this.sourceTree = parent.toString();\n        }\n\n        this.tag = tag;\n    }\n\n    StoredFileHelper(@Nullable final Context context, final DocumentFile tree,\n                     final String filename, final String mime, final boolean safe)\n            throws IOException {\n        this.docTree = tree;\n        this.context = context;\n\n        final DocumentFile res;\n\n        if (safe) {\n            // no conflicts (the filename is not in use)\n            res = this.docTree.createFile(mime, filename);\n            if (res == null) {\n                throw new IOException(\"Cannot create the file\");\n            }\n        } else {\n            res = createSAF(context, mime, filename);\n        }\n\n        this.docFile = res;\n\n        this.source = docFile.getUri().toString();\n        this.sourceTree = docTree.getUri().toString();\n\n        this.srcName = this.docFile.getName();\n        this.srcType = this.docFile.getType();\n    }\n\n    StoredFileHelper(final Path location, final String filename, final String mime)\n            throws IOException {\n        ioPath = location.resolve(filename);\n","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/TeamNewPipe/NewPipe/blob/9e8be091560a69f35d44bd252eb00bc7911c977b/app/src/main/java/org/schabi/newpipe/streams/io/StoredFileHelper.java#L74-L110","documentation":"Thrown in the 'safe' file-creation path of StoredFileHelper: the constructor calls docTree.createFile(mime, filename) on a DocumentFile tree and the call returns null, meaning the DocumentsProvider could not create a child document. The 'safe' path is used when the caller guarantees the filename is not already in use, so a null result is not a conflict — it is a provider-level failure (no permission, no space, provider error, or unsupported operation).","triggerScenarios":"StoredFileHelper is constructed with safe=true; docTree.createFile(mime, filename) returns null. This occurs when the underlying DocumentsProvider rejects the creation: the tree URI no longer grants write permission, the storage is full/read-only, the provider does not support createDocument, or the filename is illegal for the filesystem.","commonSituations":"Download target folder on an SD card that was mounted read-only or unmounted; the persistable write permission was revoked by the user or cleared on app data wipe; storage full; a cloud provider (Google Drive SAF) that does not support createFile with the requested MIME/filename; filename containing characters the provider's filesystem rejects.","solutions":["Verify write permission on the tree URI is still persisted (getPersistedUriPermissions) before attempting creation.","Check available storage space on the target volume before writing.","Sanitize the filename (strip illegal characters, reserved names) before calling createFile.","Fall back to a different storage location (app-specific external dir) if the SAF tree is unavailable."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before safe-mode file creation, verify write permission and space:\nList<UriPermission> perms = context.getContentResolver().getPersistedUriPermissions();\nboolean canWrite = perms.stream().anyMatch(p -> p.getUri().equals(treeUri) && p.isWritePermission());\nif (!canWrite || getAvailableBytes(treeUri) < minNeeded) {\n    throw new IOException(\"Cannot write to download location\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    StoredFileHelper helper = new StoredFileHelper(context, tree, filename, mime, true);\n} catch (IOException e) {\n    if (\"Cannot create the file\".equals(e.getMessage())) {\n        // provider refused creation — notify user, offer fallback location\n        promptForNewLocation();\n    } else throw e;\n}","preventionTips":["Check available disk space on the target volume before starting a download.","Sanitize filenames (strip path separators and illegal characters) before createFile.","Keep a fallback app-specific storage location when SAF tree creation fails."],"tags":["android-saf","storage","documentfile","io","file-creation"],"backgroundTag":null,"analyzedSha":"9e8be091560a69f35d44bd252eb00bc7911c977b","analyzedAt":"2026-08-14T00:11:33.519Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}