google/ExoPlayer · error · IllegalStateException

Could not create the export output file

Error message

Could not create the export output file

What it means

Thrown by TransformerActivity.createExternalCacheFile when File.createNewFile() returns false for the export output file. createNewFile fails when the file already exists (deletion was skipped or raced), the parent directory does not exist or is unwritable, or the filesystem rejects creation.

Source

Thrown at demos/transformer/src/main/java/com/google/android/exoplayer2/transformerdemo/TransformerActivity.java:363

              @Override
              public void onError(
                  Composition composition,
                  ExportResult exportResult,
                  ExportException exportException) {
                TransformerActivity.this.onError(exportException);
              }
            })
        .build();
  }

  /** Creates a cache file, resetting it if it already exists. */
  private File createExternalCacheFile(String fileName) throws IOException {
    File file = new File(getExternalCacheDir(), fileName);
    if (file.exists() && !file.delete()) {
      throw new IllegalStateException("Could not delete the previous export output file");
    }
    if (!file.createNewFile()) {
      throw new IllegalStateException("Could not create the export output file");
    }
    return file;
  }

  @RequiresNonNull({
    "inputCardView",
    "outputPlayerView",
    "exportStopwatch",
    "progressViewGroup",
  })
  private Composition createComposition(MediaItem mediaItem, @Nullable Bundle bundle)
      throws PackageManager.NameNotFoundException {
    EditedMediaItem.Builder editedMediaItemBuilder = new EditedMediaItem.Builder(mediaItem);
    // For image inputs. Automatically ignored if input is audio/video.
    editedMediaItemBuilder.setDurationUs(5_000_000).setFrameRate(30);
    boolean forceAudioTrack = false;
    if (bundle != null) {
      ImmutableList<AudioProcessor> audioProcessors = createAudioProcessorsFromBundle(bundle);

View on GitHub (pinned to dd430f7053)

Solutions

  1. Verify getExternalCacheDir() exists and canWrite() before creating the file; call mkdirs() on it if needed
  2. Use a per-run unique file name so createNewFile never collides with a stale file
  3. Free external cache space (context.getExternalCacheDir().listFiles() cleanup) before export
  4. If the file already exists, either reuse it (skip createNewFile) or delete it explicitly and confirm deletion succeeded

Example fix

// before
if (!file.createNewFile()) {
  throw new IllegalStateException("Could not create the export output file");
}

// after: tolerate an existing (already reset) file and unique-ify on failure
if (!file.createNewFile() && !file.exists()) {
  throw new IllegalStateException("Could not create the export output file");
}
Defensive patterns

Strategy: validation

Validate before calling

File dir = getExternalCacheDir();
if (dir == null || (!dir.exists() && !dir.mkdirs()) || !dir.canWrite()) {
  throw new IllegalStateException("External cache dir unavailable");
}
// storage headroom check
StatFs fs = new StatFs(dir.getAbsolutePath());
if (fs.getAvailableBytes() < REQUIRED_EXPORT_BYTES) {
  // notify user instead of letting createNewFile fail
}

Try / catch

try {
  file = createExternalCacheFile(fileName);
} catch (IllegalStateException e) {
  if (e.getMessage().contains("create")) {
    file = new File(getExternalCacheDir(),
        System.currentTimeMillis() + "-" + fileName); // unique fallback
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: createExternalCacheFile called when the target file already exists and the delete branch was not taken (file.exists() was false at check time but a concurrent creator won the race); getExternalCacheDir() returns a path that was deleted or not yet created; external cache dir full or read-only.

Common situations: Rapid repeated exports; device low on storage; external cache dir removed by the system while the activity is in background; file created by a previous crashed run between exists() and createNewFile().

Related errors


AI-assisted analysis of google/ExoPlayer@dd430f7053 (2026-08-14). Data as JSON: /api/errors/4b7791a1c61e8ac4. Report an issue: GitHub.