{"record":{"id":"e24a2981418ea037","repo":"MuntashirAkon/AppManager","slug":"could-not-open-file-for-writing","errorCode":null,"errorMessage":"Could not open file for writing: ","messagePattern":"Could not open file for writing: ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/io/PathImpl.java","lineNumber":1212,"sourceCode":"                return ((VirtualDocumentFile) documentFile).openFileDescriptor(modeBits);\n            } catch (IOException e) {\n                throw (FileNotFoundException) new FileNotFoundException(e.getMessage()).initCause(e);\n            }\n        }\n        return FileUtils.getFdFromUri(context, documentFile.getUri(), mode);\n    }\n\n    @NonNull\n    public OutputStream openOutputStream(boolean append) throws IOException {\n        DocumentFile documentFile = resolveFileOrNull(this.documentFile);\n        if (documentFile == null) {\n            throw new IOException(this.documentFile.getUri() + \" is a directory\");\n        }\n        if (documentFile instanceof ExtendedRawDocumentFile) {\n            try {\n                return Objects.requireNonNull(getFile()).newOutputStream(append);\n            } catch (IOException e) {\n                throw new IOException(\"Could not open file for writing: \" + documentFile.getUri(), e);\n            }\n        } else if (documentFile instanceof VirtualDocumentFile) {\n            return ((VirtualDocumentFile) documentFile).openOutputStream(append);\n        }\n        String mode = \"w\" + (append ? \"a\" : \"t\");\n        OutputStream os = context.getContentResolver().openOutputStream(documentFile.getUri(), mode);\n        if (os == null) {\n            throw new IOException(\"Could not resolve Uri: \" + documentFile.getUri());\n        }\n        return os;\n    }\n\n    @NonNull\n    public InputStream openInputStream() throws IOException {\n        DocumentFile documentFile = resolveFileOrNull(this.documentFile);\n        if (documentFile == null) {\n            throw new IOException(this.documentFile.getUri() + \" is a directory\");\n        }","sourceCodeStart":1194,"sourceCodeEnd":1230,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/io/PathImpl.java#L1194-L1230","documentation":"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.","triggerScenarios":"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.","commonSituations":"Read-only filesystem (system partition, mounted read-only), file locked by another process, or SELinux/permission restrictions on the raw path.","solutions":["Check path.canWrite() and the file's permissions before opening","Inspect the cause chain (getCause()) for the underlying errno/IOException","Fall back to the ContentResolver-based path or choose a writable location"],"exampleFix":"// before\nOutputStream os = path.openOutputStream();\n// after\nif (!path.canWrite()) throw new IOException(\"Not writable: \" + path.getUri());\ntry {\n    OutputStream os = path.openOutputStream();\n} catch (IOException e) {\n    Log.e(TAG, \"open failed\", e.getCause());\n    throw e;\n}","handlingStrategy":"try-catch","validationCode":"if (!path.canWrite()) throw new IOException(\"Not writable: \" + path.getUri());","typeGuard":null,"tryCatchPattern":"try {\n    OutputStream os = path.openOutputStream(append);\n} catch (IOException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Could not open file for writing\")) {\n        Log.e(TAG, \"cause\", e.getCause()); // inspect raw-file failure\n    }\n    throw e;\n}","preventionTips":["Check canWrite() and filesystem writability first","Inspect getCause() for the underlying errno","Avoid raw locations known to be read-only (e.g. system partitions)"],"tags":["io","android","stream","permissions"],"backgroundTag":"file-open-failed","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}