{"record":{"id":"a0896a8583892e99","repo":"MuntashirAkon/AppManager","slug":"could-not-create","errorCode":null,"errorMessage":"Could not create ","messagePattern":"Could not create ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/io/PathImpl.java","lineNumber":505,"sourceCode":"        if (!documentFile.isDirectory()) {\n            throw new IOException(\"Current file is not a directory.\");\n        }\n        String extension = null;\n        if (mimeType != null) {\n            extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);\n        } else mimeType = DEFAULT_MIME;\n        String nameWithExtension = displayName + (extension != null ? \".\" + extension : \"\");\n        checkVfs(Paths.appendPathSegment(documentFile.getUri(), nameWithExtension));\n        DocumentFile file = documentFile.findFile(displayName);\n        if (file != null) {\n            if (file.isDirectory()) {\n                throw new IOException(\"Directory cannot be converted to file\");\n            }\n            return new PathImpl(context, file);\n        }\n        file = documentFile.createFile(mimeType, displayName);\n        if (file == null) {\n            throw new IOException(\"Could not create \" + documentFile.getUri() + File.separatorChar + nameWithExtension + \" with type \" + mimeType);\n        }\n        return new PathImpl(context, file);\n    }\n\n    @NonNull\n    public Path findOrCreateDirectory(@NonNull String displayName) throws IOException {\n        displayName = Paths.sanitize(displayName, true);\n        if (displayName == null) {\n            throw new IOException(\"Empty display name.\");\n        }\n        if (displayName.indexOf(File.separatorChar) != -1) {\n            throw new IllegalArgumentException(\"Display name contains file separator.\");\n        }\n        DocumentFile documentFile = getRealDocumentFile(this.documentFile);\n        if (!documentFile.isDirectory()) {\n            throw new IOException(\"Current file is not a directory.\");\n        }\n        Path fsRoot = VirtualFileSystem.getFsRoot(Paths.appendPathSegment(documentFile.getUri(), displayName));","sourceCodeStart":487,"sourceCodeEnd":523,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/io/PathImpl.java#L487-L523","documentation":"If DocumentFile.createFile(mimeType, displayName) returns null — the provider failed to create the file — findOrCreateFile() throws IOException(\"Could not create <parentUri>/<nameWithExtension> with type <mimeType>\"). It reports the intended full path and MIME type, helping identify which create failed.","triggerScenarios":"documentFile.createFile() returns null: unwritable tree (revoked SAF grant, read-only volume), disallowed name, unsupported MIME type for the provider, or insufficient storage.","commonSituations":"Writing to external SD card without a current tree grant; provider not accepting the given MIME type; name with illegal characters; disk full; provider crash or returning stale state.","solutions":["Verify parent.canWrite() and that the SAF tree URI permission is still valid; re-request ACTION_OPEN_DOCUMENT_TREE if needed.","Use a standard MIME type (e.g. application/octet-stream) or let mimeType be null so DEFAULT_MIME applies.","Sanitize the filename to provider-safe characters and shorten over-long names.","Check free storage space, then retry; fall back to app-specific internal storage if the provider keeps failing."],"exampleFix":"// before\npath.findOrCreateFile(\"report\", \"application/x-custom\"); // IOException: Could not create ...\n// after\nString safeMime = isSupported(mime) ? mime : \"application/octet-stream\";\nDocumentFile parent = getRealDocumentFile(path);\nif (parent != null && parent.canWrite()) {\n    path.findOrCreateFile(\"report\", safeMime);\n}","handlingStrategy":"try-catch","validationCode":"DocumentFile parent = getRealDocumentFile(path);\nboolean ok = parent != null && parent.canWrite() && isSafeFileName(name) && hasFreeSpace();","typeGuard":null,"tryCatchPattern":"try {\n    return path.findOrCreateFile(name, mime);\n} catch (IOException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Could not create \")) {\n        return path.findOrCreateFile(name, null); // retry with DEFAULT_MIME\n    }\n    throw e;\n}","preventionTips":["Verify canWrite() and persisted SAF grants","Use standard MIME types or null for default","Monitor free storage before large writes","Fall back to internal app storage on provider failures"],"tags":["android","saf","filesystem","file-creation"],"backgroundTag":"file-write-failed","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}