{"record":{"id":"173a9e8d51a30260","repo":"Yalantis/uCrop","slug":"save-tiff-failed-to-open-file-s-for-writing","errorCode":null,"errorMessage":"save_tiff(): Failed to open file '%s' for writing.","messagePattern":"save_tiff\\(\\): Failed to open file '(.+?)' for writing\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":62728,"sourceCode":"     **/\n    const CImg<T>& save_tiff(const char *const filename, const unsigned int compression_type=0,\n                             const float *const voxel_size=0, const char *const description=0,\n                             const bool use_bigtiff=true) const {\n      if (!filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_tiff(): Specified filename is (null).\",\n                                    cimg_instance);\n      if (is_empty()) { cimg::fempty(0,filename); return *this; }\n\n#ifdef cimg_use_tiff\n      const bool\n        _use_bigtiff = use_bigtiff && sizeof(ulongT)>=8 && size()*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, val_max = (double)max_min(val_min);\n        cimg_forZ(*this,z) _save_tiff(tif,z,z,compression_type,voxel_size,description,val_min,val_max);\n        TIFFClose(tif);\n      } else throw CImgIOException(_cimg_instance\n                                   \"save_tiff(): Failed to open file '%s' for writing.\",\n                                   cimg_instance,\n                                   filename);\n      return *this;\n#else\n      cimg::unused(compression_type,voxel_size,description,use_bigtiff);\n      return save_other(filename);\n#endif\n    }\n\n#ifdef cimg_use_tiff\n\n#define _cimg_save_tiff(types,typed) if (!std::strcmp(types,pixel_type())) { \\\n    const typed foo = (typed)0; return _save_tiff(tif,directory,z,foo,compression_type,voxel_size,description,\\\n                                                  val_min,val_max); }\n\n    // [internal] Save a plane into a tiff file.\n    template<typename t>","sourceCodeStart":62710,"sourceCodeEnd":62746,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L62710-L62746","documentation":"After the null check, save_tiff() opens the file via TIFFOpen (libtiff) in \"w4\" or \"w8\" mode; if TIFFOpen returns NULL it throws CImgIOException that the file could not be opened for writing. This is an OS/libtiff-level open failure surfaced as a CImg exception.","triggerScenarios":"save_tiff() on a path in a non-existent directory; no write permission; disk full or I/O error; path too long or invalid characters; read-only filesystem (e.g. Android internal dir).","commonSituations":"Writing to /sdcard root on newer Android without permission; output directory never created; filename with typos; sandboxed app without storage permission.","solutions":["Create the output directory (mkdir -p equivalent) before saving","Check file/path permissions and request storage permission on mobile","Verify the filename is a valid, writable path","Check free disk space","Test TIFFOpen/TIFFOpen-style open separately to get the OS errno"],"exampleFix":"// before\nimg.save_tiff(\"/data/out/result.tif\"); // dir may not exist\n// after\nstd::system(\"mkdir -p /data/out\"); // or std::filesystem::create_directories\nimg.save_tiff(\"/data/out/result.tif\");","handlingStrategy":"try-catch","validationCode":"std::filesystem::path p(outPath);\nstd::filesystem::create_directories(p.parent_path());\nFILE *probe = fopen(outPath, \"wb\"); if (!probe) throw std::runtime_error(\"unwritable tiff path\"); fclose(probe);","typeGuard":"bool canOpenForWrite(const char *p) { if (!p || !*p) return false; FILE *f = fopen(p, \"wb\"); if (!f) return false; fclose(f); return true; }","tryCatchPattern":"try { img.save_tiff(path.c_str(), comp); } catch (CImgIOException &e) { fprintf(stderr, \"tiff open failed: %s\\n\", e.what()); }","preventionTips":["Create output directories before saving","Request/check storage permissions on mobile platforms","Test writability with a probe open","Watch disk space for large TIFFs"],"tags":["tiff","file-open","permissions","libtiff"],"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"}