{"record":{"id":"afc272eb061fca3c","repo":"google/ExoPlayer","slug":"could-not-delete-the-previous-export-output-file","errorCode":null,"errorMessage":"Could not delete the previous export output file","messagePattern":"Could not delete the previous export output file","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"demos/transformer/src/main/java/com/google/android/exoplayer2/transformerdemo/TransformerActivity.java","lineNumber":360,"sourceCode":"                TransformerActivity.this.onCompleted(inputUri, filePath);\n              }\n\n              @Override\n              public void onError(\n                  Composition composition,\n                  ExportResult exportResult,\n                  ExportException exportException) {\n                TransformerActivity.this.onError(exportException);\n              }\n            })\n        .build();\n  }\n\n  /** Creates a cache file, resetting it if it already exists. */\n  private File createExternalCacheFile(String fileName) throws IOException {\n    File file = new File(getExternalCacheDir(), fileName);\n    if (file.exists() && !file.delete()) {\n      throw new IllegalStateException(\"Could not delete the previous export output file\");\n    }\n    if (!file.createNewFile()) {\n      throw new IllegalStateException(\"Could not create the export output file\");\n    }\n    return file;\n  }\n\n  @RequiresNonNull({\n    \"inputCardView\",\n    \"outputPlayerView\",\n    \"exportStopwatch\",\n    \"progressViewGroup\",\n  })\n  private Composition createComposition(MediaItem mediaItem, @Nullable Bundle bundle)\n      throws PackageManager.NameNotFoundException {\n    EditedMediaItem.Builder editedMediaItemBuilder = new EditedMediaItem.Builder(mediaItem);\n    // For image inputs. Automatically ignored if input is audio/video.\n    editedMediaItemBuilder.setDurationUs(5_000_000).setFrameRate(30);","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/google/ExoPlayer/blob/dd430f7053a1a3958deea3ead6a0565150c06bfc/demos/transformer/src/main/java/com/google/android/exoplayer2/transformerdemo/TransformerActivity.java#L342-L378","documentation":"Thrown by TransformerActivity.createExternalCacheFile when a previous export output file exists in the app's external cache dir but File.delete() returns false. This is a demo-activity guard that resets the output file before a new export so Transformer does not append to or fail on stale output. The delete fails when something still holds the file open or the storage is in a bad state.","triggerScenarios":"Calling startExport/createExternalCacheFile while a previous export's output file is still open by a Player or Transformer; running a second export before releasing the output player; external cache dir mounted read-only or full; file deleted concurrently between exists() and delete().","commonSituations":"User re-exports immediately after a previous run while the output PlayerView still plays the old file; process crashed leaving a locked file; device storage pressure; permissions changed on getExternalCacheDir().","solutions":["Release any Player and Transformer instance (and stop playback of the output file) before creating a new export file","Check file.canWrite() and external storage state before export; surface a user-facing message instead of crashing","Retry the delete once after a short delay, or fall back to a uniquely named file (e.g. append System.currentTimeMillis()) when deletion keeps failing","Call file.delete() in onDestroy/onStop so the demo starts clean next time"],"exampleFix":"// before\nif (file.exists() && !file.delete()) {\n  throw new IllegalStateException(\"Could not delete the previous export output file\");\n}\n\n// after: release the player that holds the file, then retry with a unique name\nif (file.exists() && !file.delete()) {\n  String unique = fileName + '-' + System.currentTimeMillis();\n  file = new File(getExternalCacheDir(), unique);\n}\nif (!file.createNewFile()) {\n  throw new IllegalStateException(\"Could not create the export output file\");\n}","handlingStrategy":"try-catch","validationCode":"File file = new File(context.getExternalCacheDir(), fileName);\nif (file.exists() && (!file.canWrite() || !file.delete())) {\n  // do not proceed: someone still holds the file or storage is bad\n  return new File(context.getExternalCacheDir(),\n      System.currentTimeMillis() + \"-\" + fileName);\n}","typeGuard":null,"tryCatchPattern":"try {\n  File out = createExternalCacheFile(fileName);\n} catch (IllegalStateException e) {\n  // message tells which step failed; release players and retry with a unique name\n  releasePlayersAndTransformer();\n  File out = new File(getExternalCacheDir(),\n      System.currentTimeMillis() + \"-\" + fileName);\n}","preventionTips":["Always release the Transformer and any Player referencing the output file before starting a new export","Never reuse a fixed output file name across rapid re-exports; include a timestamp","Clean the external cache dir on app start so stale files cannot accumulate"],"tags":["android","transformer","file-io","demo"],"backgroundTag":null,"analyzedSha":"dd430f7053a1a3958deea3ead6a0565150c06bfc","analyzedAt":"2026-08-14T12:22:02.982Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}