diff --git a/packages/camera/camera_windows/CHANGELOG.md b/packages/camera/camera_windows/CHANGELOG.md index 7316cdb8b1e..9186f9c1cd6 100644 --- a/packages/camera/camera_windows/CHANGELOG.md +++ b/packages/camera/camera_windows/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.6+5 + +* Use temp directory fallback when Pictures folder is unavailable + ## 0.2.6+4 * Removes usage of the deprecated and ignored `maxVideoDuration` in the example. diff --git a/packages/camera/camera_windows/pubspec.yaml b/packages/camera/camera_windows/pubspec.yaml index ab42277ea39..e6341076d86 100644 --- a/packages/camera/camera_windows/pubspec.yaml +++ b/packages/camera/camera_windows/pubspec.yaml @@ -2,7 +2,7 @@ name: camera_windows description: A Flutter plugin for getting information about and controlling the camera on Windows. repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_windows issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22 -version: 0.2.6+4 +version: 0.2.6+5 environment: sdk: ^3.8.0 diff --git a/packages/camera/camera_windows/windows/camera_plugin.cpp b/packages/camera/camera_windows/windows/camera_plugin.cpp index c01db2603ca..fffe8558327 100644 --- a/packages/camera/camera_windows/windows/camera_plugin.cpp +++ b/packages/camera/camera_windows/windows/camera_plugin.cpp @@ -86,17 +86,34 @@ std::string GetCurrentTimeString() { } // Builds file path for picture capture. +/// If Pictures folder exist use it, otherwise use temp directory +/// This fallback prevents crashes or unexpected failures on systems where the Pictures folder is unavailable or restricted std::optional GetFilePathForPicture() { ComHeapPtr known_folder_path; HRESULT hr = SHGetKnownFolderPath(FOLDERID_Pictures, KF_FLAG_CREATE, nullptr, &known_folder_path); - if (FAILED(hr)) { - return std::nullopt; + + std::wstring wpath; + + if (SUCCEEDED(hr)) { + wpath = std::wstring(known_folder_path); + } else { + // Fallback to temp folder + wchar_t tempPath[MAX_PATH]; + DWORD len = GetTempPathW(MAX_PATH, tempPath); + if (len == 0 || len > MAX_PATH) { + return std::nullopt; + } + wpath = std::wstring(tempPath); } - std::string path = Utf8FromUtf16(std::wstring(known_folder_path)); + if (!wpath.empty() && wpath.back() != L'\\' && wpath.back() != L'/') { + wpath.push_back(L'\\'); + } + + std::string path = Utf8FromUtf16(wpath); - return path + "\\" + "PhotoCapture_" + GetCurrentTimeString() + "." + + return path + "PhotoCapture_" + GetCurrentTimeString() + "." + kPictureCaptureExtension; }