MuntashirAkon/AppManager · error · IOException

Could not create

Error message

Could not create 

What it means

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.

Source

Thrown at app/src/main/java/io/github/muntashirakon/io/PathImpl.java:505

        if (!documentFile.isDirectory()) {
            throw new IOException("Current file is not a directory.");
        }
        String extension = null;
        if (mimeType != null) {
            extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
        } else mimeType = DEFAULT_MIME;
        String nameWithExtension = displayName + (extension != null ? "." + extension : "");
        checkVfs(Paths.appendPathSegment(documentFile.getUri(), nameWithExtension));
        DocumentFile file = documentFile.findFile(displayName);
        if (file != null) {
            if (file.isDirectory()) {
                throw new IOException("Directory cannot be converted to file");
            }
            return new PathImpl(context, file);
        }
        file = documentFile.createFile(mimeType, displayName);
        if (file == null) {
            throw new IOException("Could not create " + documentFile.getUri() + File.separatorChar + nameWithExtension + " with type " + mimeType);
        }
        return new PathImpl(context, file);
    }

    @NonNull
    public Path findOrCreateDirectory(@NonNull String displayName) throws IOException {
        displayName = Paths.sanitize(displayName, true);
        if (displayName == null) {
            throw new IOException("Empty display name.");
        }
        if (displayName.indexOf(File.separatorChar) != -1) {
            throw new IllegalArgumentException("Display name contains file separator.");
        }
        DocumentFile documentFile = getRealDocumentFile(this.documentFile);
        if (!documentFile.isDirectory()) {
            throw new IOException("Current file is not a directory.");
        }
        Path fsRoot = VirtualFileSystem.getFsRoot(Paths.appendPathSegment(documentFile.getUri(), displayName));

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Verify parent.canWrite() and that the SAF tree URI permission is still valid; re-request ACTION_OPEN_DOCUMENT_TREE if needed.
  2. Use a standard MIME type (e.g. application/octet-stream) or let mimeType be null so DEFAULT_MIME applies.
  3. Sanitize the filename to provider-safe characters and shorten over-long names.
  4. Check free storage space, then retry; fall back to app-specific internal storage if the provider keeps failing.

Example fix

// before
path.findOrCreateFile("report", "application/x-custom"); // IOException: Could not create ...
// after
String safeMime = isSupported(mime) ? mime : "application/octet-stream";
DocumentFile parent = getRealDocumentFile(path);
if (parent != null && parent.canWrite()) {
    path.findOrCreateFile("report", safeMime);
}
Defensive patterns

Strategy: try-catch

Validate before calling

DocumentFile parent = getRealDocumentFile(path);
boolean ok = parent != null && parent.canWrite() && isSafeFileName(name) && hasFreeSpace();

Try / catch

try {
    return path.findOrCreateFile(name, mime);
} catch (IOException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Could not create ")) {
        return path.findOrCreateFile(name, null); // retry with DEFAULT_MIME
    }
    throw e;
}

Prevention

When it happens

Trigger: documentFile.createFile() returns null: unwritable tree (revoked SAF grant, read-only volume), disallowed name, unsupported MIME type for the provider, or insufficient storage.

Common situations: 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.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/a0896a8583892e99. Report an issue: GitHub.