Skip to content

Commit 9b4f58e

Browse files
committed
🐛 Fix potential non exist directory access.
1 parent ae90574 commit 9b4f58e

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

lib/src/widget/camera_picker.dart

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,21 +277,34 @@ class CameraPickerState extends State<CameraPicker> {
277277
/// * SDK < 29: /sdcard/DCIM/camera .
278278
/// * SDK >= 29: ${cacheDir}/ .
279279
Future<void> initStorePath() async {
280-
/// Get device info before the path initialized.
281-
await DeviceUtils.getDeviceInfo();
280+
try {
281+
/// Get device info before the path initialized.
282+
await DeviceUtils.getDeviceInfo();
282283

283-
if (Platform.isAndroid) {
284-
if (DeviceUtils.isLowerThanAndroidQ) {
285-
cacheFilePath =
286-
'${(await getExternalStorageDirectory()).path}/DCIM/Camera/';
284+
if (Platform.isAndroid) {
285+
if (DeviceUtils.isLowerThanAndroidQ) {
286+
cacheFilePath =
287+
'${(await getExternalStorageDirectory()).path}/DCIM/Camera/';
288+
} else {
289+
cacheFilePath = (await getTemporaryDirectory()).path;
290+
}
287291
} else {
288-
cacheFilePath = (await getTemporaryDirectory()).path;
292+
cacheFilePath = (await getApplicationDocumentsDirectory()).path;
289293
}
290-
} else {
291-
cacheFilePath = (await getApplicationDocumentsDirectory()).path;
292-
}
293-
if (cacheFilePath != null) {
294-
cacheFilePath += '/cameraPicker';
294+
if (cacheFilePath != null) {
295+
cacheFilePath += '/cameraPicker';
296+
297+
/// Check if the directory is exist.
298+
final Directory directory = Directory(cacheFilePath);
299+
if (!directory.existsSync()) {
300+
/// Create the directory recursively.
301+
await directory.create(recursive: true);
302+
}
303+
} else {
304+
realDebugPrint('Failed to initialize path: Still null.');
305+
}
306+
} catch (e) {
307+
realDebugPrint('Error when initializing store path: $e');
295308
}
296309
}
297310

0 commit comments

Comments
 (0)