Skip to content

Commit d9b14d2

Browse files
committed
♻️ Remove shouldKeptInLocal and update path methods.
1 parent 12c8dcd commit d9b14d2

File tree

7 files changed

+80
-166
lines changed

7 files changed

+80
-166
lines changed

lib/src/constants/constants.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export 'package:flutter_common_exports/flutter_common_exports.dart';
88
export 'package:photo_manager/photo_manager.dart';
99

1010
export '../delegates/camera_picker_text_delegate.dart';
11+
export '../utils/device_utils.dart';
1112
export 'colors.dart';
1213

1314
class Constants {

lib/src/utils/device_utils.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
///
2+
/// [Author] Alex (https://github.com/AlexV525)
3+
/// [Date] 2020/7/17 10:09
4+
///
5+
import 'dart:io';
6+
7+
import 'package:device_info/device_info.dart';
8+
9+
export 'package:device_info/device_info.dart';
10+
11+
class DeviceUtils {
12+
const DeviceUtils._();
13+
14+
static final DeviceInfoPlugin _deviceInfoPlugin = DeviceInfoPlugin();
15+
static dynamic deviceInfo;
16+
17+
static Future<void> getDeviceInfo() async {
18+
if (Platform.isAndroid) {
19+
deviceInfo = await _deviceInfoPlugin.androidInfo;
20+
} else if (Platform.isIOS) {
21+
deviceInfo = await _deviceInfoPlugin.iosInfo;
22+
} else {
23+
throw UnsupportedError('Platform not supported');
24+
}
25+
}
26+
27+
static bool get isLowerThanAndroidQ {
28+
return (deviceInfo as AndroidDeviceInfo).version.sdkInt < 29;
29+
}
30+
}

lib/src/widget/camera_picker.dart

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import 'camera_picker_viewer.dart';
2828
class CameraPicker extends StatefulWidget {
2929
CameraPicker({
3030
Key key,
31-
this.shouldKeptInLocal = false,
3231
this.isAllowRecording = false,
3332
this.maximumRecordingDuration = const Duration(seconds: 15),
3433
this.theme,
@@ -40,10 +39,6 @@ class CameraPicker extends StatefulWidget {
4039
: DefaultCameraPickerTextDelegate());
4140
}
4241

43-
/// Whether the taken file should be kept in local.
44-
/// 拍照的文件是否应该保存在本地
45-
final bool shouldKeptInLocal;
46-
4742
/// Whether the picker can record video.
4843
/// 选择器是否可以录像
4944
final bool isAllowRecording;
@@ -75,7 +70,6 @@ class CameraPicker extends StatefulWidget {
7570
).push<AssetEntity>(
7671
SlidePageTransitionBuilder<AssetEntity>(
7772
builder: CameraPicker(
78-
shouldKeptInLocal: shouldKeptInLocal,
7973
isAllowRecording: isAllowRecording,
8074
theme: theme,
8175
textDelegate: textDelegate,
@@ -195,10 +189,6 @@ class CameraPickerState extends State<CameraPicker> {
195189
/// 当前的相机实例是否已完成初始化
196190
bool get isInitialized => cameraController?.value?.isInitialized ?? false;
197191

198-
/// Whether the taken file should be kept in local. (A non-null wrapper)
199-
/// 拍照的文件是否应该保存在本地(非空包装)
200-
bool get shouldKeptInLocal => widget.shouldKeptInLocal ?? false;
201-
202192
/// Whether the picker can record video. (A non-null wrapper)
203193
/// 选择器是否可以录像(非空包装)
204194
bool get isAllowRecording => widget.isAllowRecording ?? false;
@@ -265,25 +255,29 @@ class CameraPickerState extends State<CameraPicker> {
265255
super.dispose();
266256
}
267257

268-
/// Defined the path according to [shouldKeptInLocal], with platforms specification.
269-
/// 根据 [shouldKeptInLocal] 及平台规定确定存储路径
258+
/// Defined the path with platforms specification.
259+
/// 根据平台存储规范及特性确定存储路径
270260
///
271-
/// * When [Platform.isIOS], use [getApplicationDocumentsDirectory].
272-
/// * When platform is others: [shouldKeptInLocal] ?
273-
/// * [true] : [getExternalStorageDirectory]'s path
274-
/// * [false]: [getExternalCacheDirectories]'s last path
261+
/// * When the platform is not Android, use [getApplicationDocumentsDirectory] .
262+
/// * When [Platform.isAndroid] :
263+
/// * SDK < 29: /sdcard/DCIM/camera .
264+
/// * SDK >= 29: ${cacheDir}/ .
275265
Future<void> initStorePath() async {
276-
if (Platform.isIOS) {
277-
cacheFilePath = (await getApplicationDocumentsDirectory()).path;
278-
} else {
279-
if (shouldKeptInLocal) {
280-
cacheFilePath = (await getExternalStorageDirectory()).path;
266+
/// Get device info before the path initialized.
267+
await DeviceUtils.getDeviceInfo();
268+
269+
if (Platform.isAndroid) {
270+
if (DeviceUtils.isLowerThanAndroidQ) {
271+
cacheFilePath =
272+
'${(await getExternalStorageDirectory()).path}/DCIM/Camera/';
281273
} else {
282-
cacheFilePath = (await getExternalCacheDirectories()).last.path;
274+
cacheFilePath = (await getTemporaryDirectory()).path;
283275
}
276+
} else {
277+
cacheFilePath = (await getApplicationDocumentsDirectory()).path;
284278
}
285279
if (cacheFilePath != null) {
286-
cacheFilePath += '/picker';
280+
cacheFilePath += '/cameraPicker';
287281
}
288282
}
289283

lib/src/widget/camera_picker_viewer.dart

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class CameraPickerViewer extends StatefulWidget {
6767
previewFilePath: previewFilePath,
6868
theme: theme,
6969
);
70-
final PageRouteBuilder<AssetEntity> pageRoute = PageRouteBuilder<AssetEntity>(
70+
final PageRouteBuilder<AssetEntity> pageRoute =
71+
PageRouteBuilder<AssetEntity>(
7172
pageBuilder: (
7273
BuildContext context,
7374
Animation<double> animation,
@@ -84,7 +85,8 @@ class CameraPickerViewer extends StatefulWidget {
8485
return FadeTransition(opacity: animation, child: child);
8586
},
8687
);
87-
final AssetEntity result = await Navigator.of(context).push<AssetEntity>(pageRoute);
88+
final AssetEntity result =
89+
await Navigator.of(context).push<AssetEntity>(pageRoute);
8890
return result;
8991
} catch (e) {
9092
realDebugPrint('Error when calling camera picker viewer: $e');
@@ -193,27 +195,38 @@ class _CameraPickerViewerState extends State<CameraPickerViewer> {
193195
}
194196
}
195197

196-
/// When users confirm to use the taken file, create the [AssetEntity], then delete
197-
/// the file if not [shouldKeptInLocal]. While the entity might returned null, there's
198-
/// no side effects if popping `null` because the parent picker will ignore it.
198+
/// When users confirm to use the taken file, create the [AssetEntity].
199+
/// While the entity might returned null, there's no side effects if popping `null`
200+
/// because the parent picker will ignore it.
199201
Future<void> createAssetEntityAndPop() async {
200202
try {
201203
Future<AssetEntity> saveFuture;
202204

203205
switch (pickerType) {
204206
case CameraPickerViewType.image:
205207
final Uint8List data = await previewFile.readAsBytes();
206-
saveFuture = PhotoManager.editor.saveImage(data, title: previewFilePath);
207-
break;
208+
saveFuture = PhotoManager.editor.saveImage(
209+
data,
210+
title: previewFilePath,
211+
);
208212
break;
209213
case CameraPickerViewType.video:
210-
saveFuture = PhotoManager.editor.saveVideo(previewFile, title: previewFilePath);
214+
saveFuture = PhotoManager.editor.saveVideo(
215+
previewFile,
216+
title: previewFilePath,
217+
);
211218
break;
212219
}
213220

214221
saveFuture.then((AssetEntity entity) {
215-
if (!pickerState.shouldKeptInLocal) {
216-
previewFile.delete();
222+
if (Platform.isAndroid) {
223+
if (!DeviceUtils.isLowerThanAndroidQ && previewFile.existsSync()) {
224+
previewFile.delete();
225+
}
226+
} else {
227+
if (previewFile.existsSync()) {
228+
previewFile.delete();
229+
}
217230
}
218231
Navigator.of(context).pop(entity);
219232
});
@@ -228,10 +241,9 @@ class _CameraPickerViewerState extends State<CameraPickerViewer> {
228241
return InkWell(
229242
borderRadius: maxBorderRadius,
230243
onTap: () {
231-
previewFile.delete();
232-
widget.pickerState.setState(() {
233-
widget.pickerState.takenPictureFilePath = null;
234-
});
244+
if (previewFile.existsSync()) {
245+
previewFile.delete();
246+
}
235247
Navigator.of(context).pop();
236248
},
237249
child: Container(
@@ -295,7 +307,9 @@ class _CameraPickerViewerState extends State<CameraPickerViewer> {
295307
shape: BoxShape.circle,
296308
),
297309
child: Icon(
298-
isControllerPlaying ? Icons.pause_circle_outline : Icons.play_circle_filled,
310+
isControllerPlaying
311+
? Icons.pause_circle_outline
312+
: Icons.play_circle_filled,
299313
size: 70.0,
300314
color: Colors.white,
301315
),
@@ -357,7 +371,8 @@ class _CameraPickerViewerState extends State<CameraPickerViewer> {
357371
),
358372

359373
/// Place the button before the actions to ensure it's not blocking.
360-
if (pickerType == CameraPickerViewType.video && videoController != null)
374+
if (pickerType == CameraPickerViewType.video &&
375+
videoController != null)
361376
playControlButton,
362377

363378
viewerActions,

lib/src/widget/temp_picker.dart

Lines changed: 0 additions & 126 deletions
This file was deleted.

lib/wechat_camera_picker.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ export 'package:photo_manager/photo_manager.dart';
44

55
export 'src/delegates/camera_picker_text_delegate.dart';
66
export 'src/widget/camera_picker.dart';
7-
export 'src/widget/temp_picker.dart';

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies:
1212
sdk: flutter
1313

1414
camera: ^0.5.8+2
15+
device_info: ^0.4.2+4
1516
flutter_common_exports: ^0.1.0
1617
path_provider: ^1.6.11
1718
photo_manager: ^0.5.6

0 commit comments

Comments
 (0)