MuntashirAkon/AppManager · error · IOException

Failed to create directory for " + mNewName

Error message

Failed to create directory for " + mNewName

What it means

AtomicExtendedFile.startWrite opens a new output stream for the temp/new file; if the parent directory doesn't exist it attempts mkdirs(), and when directory creation fails it throws IOException "Failed to create directory for <mNewName>" (wrapping the original FileNotFoundException).

Solutions

  1. Create/verify the parent directory beforehand and check storage permissions (request runtime storage access)
  2. Ensure no regular file exists at a parent path segment (delete/rename it)
  3. Check available storage and mount state (read-only volume)
  4. Point the atomic write at a directory the app can write (getFilesDir/getCacheDir)

Example fix

// before
atomicFile.startWrite(); // throws if dir missing & uncreatable
// after
File dir = target.getParentFile();
if (dir != null && !dir.exists() && !dir.mkdirs()) {
    throw new IOException("Cannot create dir " + dir + ": check permissions/storage");
}
OutputStream os = atomicFile.startWrite();
Defensive patterns

Strategy: try-catch

Validate before calling

File dir = target.getParentFile();
boolean writable = dir != null
    && (dir.exists() || dir.mkdirs())
    && dir.canWrite();

Try / catch

try {
    OutputStream os = atomicFile.startWrite();
} catch (IOException e) {
    Log.e(TAG, "Cannot create target dir/file — check permissions and storage", e);
}

Prevention

When it happens

Trigger: Calling startWrite (e.g. via doWriteState) when the target file's parent directory does not exist and cannot be created — due to missing write permission on an ancestor, a non-directory occupying the path, or read-only/external storage.

Common situations: Writing to a path on read-only storage or SD card without permissions; parent path component is actually a file; missing MANAGE_EXTERNAL_STORAGE/legacy storage access on newer Android; path typo creating a deep nonexistent directory.

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/d86b7504d0d4cfd4. Report an issue: GitHub.

Appendix: source

Thrown at libcore/io/src/main/java/io/github/muntashirakon/io/AtomicExtendedFile.java:95

     * a write, this will simply replace whatever that thread is writing
     * with the new file being written by this thread, and when the other
     * thread finishes the write the new write operation will no longer be
     * safe (or will be lost).  You must do your own threading protection for
     * access to AtomicFile.
     */
    @WorkerThread
    @NonNull
    public FileOutputStream startWrite() throws IOException {
        if (mLegacyBackupName.exists()) {
            rename(mLegacyBackupName, mBaseName);
        }

        try {
            return mNewName.newOutputStream();
        } catch (FileNotFoundException e) {
            File parent = mNewName.getParentFile();
            if (!parent.mkdirs()) {
                throw new IOException("Failed to create directory for " + mNewName, e);
            }
            try {
                return mNewName.newOutputStream();
            } catch (FileNotFoundException e2) {
                throw new IOException("Failed to create new file " + mNewName, e2);
            }
        }
    }

    /**
     * Call when you have successfully finished writing to the stream
     * returned by {@link #startWrite()}.  This will close, sync, and
     * commit the new data.  The next attempt to read the atomic file
     * will return the new file stream.
     */
    public void finishWrite(@Nullable FileOutputStream str) {
        if (str == null) {
            return;

View on GitHub (pinned to 0152f468fc)