{"record":{"id":"ae6c1f6562309896","repo":"Yalantis/uCrop","slug":"save-tiff-failed-to-open-stream-for-file-s","errorCode":null,"errorMessage":"save_tiff(): Failed to open stream for file '%s'.","messagePattern":"save_tiff\\(\\): Failed to open stream for file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":68465,"sourceCode":"#else\n      ulongT siz = 0;\n      cimglist_for(*this,l) siz+=_data[l].size();\n      const bool _use_bigtiff = use_bigtiff && sizeof(siz)>=8 && siz*sizeof(T)>=1UL<<31; // No bigtiff for small images\n      TIFF *tif = TIFFOpen(filename,_use_bigtiff?\"w8\":\"w4\");\n      if (tif) {\n        double val_min = cimg::type<double>::inf(), val_max = -val_min;\n        cimglist_for(*this,l) {\n          double l_val_min, l_val_max = (double)_data[l].max_min(l_val_min);\n          if (l_val_min<val_min) val_min = l_val_min;\n          if (l_val_max>val_max) val_max = l_val_max;\n        }\n        for (unsigned int dir = 0, l = 0; l<_width; ++l) {\n          const CImg<T>& img = (*this)[l];\n          cimg_forZ(img,z) img._save_tiff(tif,dir++,z,compression_type,voxel_size,description,val_min,val_max);\n        }\n        TIFFClose(tif);\n      } else\n        throw CImgIOException(_cimglist_instance\n                              \"save_tiff(): Failed to open stream for file '%s'.\",\n                              cimglist_instance,\n                              filename);\n#endif\n      return *this;\n    }\n\n    //! Save list as a gzipped file, using external tool 'gzip'.\n    /**\n      \\param filename Filename to write data to.\n    **/\n    const CImgList<T>& save_gzip_external(const char *const filename) const {\n      if (!filename)\n        throw CImgIOException(_cimglist_instance\n                              \"save_gzip_external(): Specified filename is (null).\",\n                              cimglist_instance);\n      CImg<charT> command(1024), filename_tmp(256), body(256);\n      const char","sourceCodeStart":68447,"sourceCodeEnd":68483,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L68447-L68483","documentation":"save_tiff calls TIFFOpen (libtiff) on the given filename; if libtiff cannot open the file for writing (bad path, missing directory, permissions, read-only filesystem), the returned TIFF* is null and CImgIOException 'Failed to open stream' is thrown. This is an OS-level open failure surfaced through libtiff.","triggerScenarios":"save_tiff with a path in a non-existent directory, no write permission, an unwritable device, or a filename too long/invalid for the OS; also when TIFF support was compiled in but the path is a directory.","commonSituations":"Read-only USB/app storage (especially Android JNI, as here under ucrop/jni); wrong output directory; sandboxed process lacking storage permission.","solutions":["Ensure the target directory exists and is writable (create it, e.g. cimg::mkdir or std::filesystem::create_directories).","Check file permissions / storage permissions of the process before saving.","Try saving to an app-writable location (e.g. cache dir) and move the file afterwards."],"exampleFix":"// before\nlist.save_tiff(\"/sdcard/out.tif\"); // no storage permission\n// after\nstd::filesystem::create_directories(\"/sdcard/DCIM/myapp\");\nif (std::filesystem::is_directory(\"/sdcard/DCIM/myapp\"))\n  list.save_tiff(\"/sdcard/DCIM/myapp/out.tif\");","handlingStrategy":"try-catch","validationCode":"// ensure directory exists and is writable before saving\nstd::filesystem::path p(filename);\nif (p.has_parent_path()) std::filesystem::create_directories(p.parent_path());\nFILE *t = std::fopen(filename, \"ab\"); bool writable = t != nullptr; if (t) std::fclose(t);","typeGuard":null,"tryCatchPattern":"try { list.save_tiff(filename); } catch (CImgIOException &e) { std::fprintf(stderr, \"cannot open %s: %s\", filename, e.what()); /* fallback path */ }","preventionTips":["Create the output directory before saving","Request/check storage permissions on Android","Use app-writable cache directories, then move files","Catch CImgIOException around all file saves"],"tags":["file-io","libtiff","permissions"],"backgroundTag":"file-open-failed","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}