MuntashirAkon/AppManager · error · IOException

Could not open file for writing:

Error message

Could not open file for writing: 

What it means

For ExtendedRawDocumentFile documents, openOutputStream() delegates to getFile().newOutputStream(append); if that throws IOException, it is rethrown as IOException("Could not open file for writing: <uri>", e) with the original cause attached. The raw-file backing could not produce a writable stream.

Source

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

                return ((VirtualDocumentFile) documentFile).openFileDescriptor(modeBits);
            } catch (IOException e) {
                throw (FileNotFoundException) new FileNotFoundException(e.getMessage()).initCause(e);
            }
        }
        return FileUtils.getFdFromUri(context, documentFile.getUri(), mode);
    }

    @NonNull
    public OutputStream openOutputStream(boolean append) throws IOException {
        DocumentFile documentFile = resolveFileOrNull(this.documentFile);
        if (documentFile == null) {
            throw new IOException(this.documentFile.getUri() + " is a directory");
        }
        if (documentFile instanceof ExtendedRawDocumentFile) {
            try {
                return Objects.requireNonNull(getFile()).newOutputStream(append);
            } catch (IOException e) {
                throw new IOException("Could not open file for writing: " + documentFile.getUri(), e);
            }
        } else if (documentFile instanceof VirtualDocumentFile) {
            return ((VirtualDocumentFile) documentFile).openOutputStream(append);
        }
        String mode = "w" + (append ? "a" : "t");
        OutputStream os = context.getContentResolver().openOutputStream(documentFile.getUri(), mode);
        if (os == null) {
            throw new IOException("Could not resolve Uri: " + documentFile.getUri());
        }
        return os;
    }

    @NonNull
    public InputStream openInputStream() throws IOException {
        DocumentFile documentFile = resolveFileOrNull(this.documentFile);
        if (documentFile == null) {
            throw new IOException(this.documentFile.getUri() + " is a directory");
        }

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Check path.canWrite() and the file's permissions before opening
  2. Inspect the cause chain (getCause()) for the underlying errno/IOException
  3. Fall back to the ContentResolver-based path or choose a writable location

Example fix

// before
OutputStream os = path.openOutputStream();
// after
if (!path.canWrite()) throw new IOException("Not writable: " + path.getUri());
try {
    OutputStream os = path.openOutputStream();
} catch (IOException e) {
    Log.e(TAG, "open failed", e.getCause());
    throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (!path.canWrite()) throw new IOException("Not writable: " + path.getUri());

Try / catch

try {
    OutputStream os = path.openOutputStream(append);
} catch (IOException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Could not open file for writing")) {
        Log.e(TAG, "cause", e.getCause()); // inspect raw-file failure
    }
    throw e;
}

Prevention

When it happens

Trigger: Opening an output stream on a Path backed by ExtendedRawDocumentFile (raw local file under this library's extended wrapper) when newOutputStream fails — unwritable file, missing permissions, full disk, or the File object is null/unavailable.

Common situations: Read-only filesystem (system partition, mounted read-only), file locked by another process, or SELinux/permission restrictions on the raw path.

Understand the failure class

Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.

Related errors


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