diff --git a/packages/image_picker/image_picker_ohos/example/lib/main.dart b/packages/image_picker/image_picker_ohos/example/lib/main.dart index fd568fbc1eeb841e04f454f82ebe1e92f4626903..803cd8e55371bd2b359164936db6f4d057943554 100644 --- a/packages/image_picker/image_picker_ohos/example/lib/main.dart +++ b/packages/image_picker/image_picker_ohos/example/lib/main.dart @@ -74,18 +74,18 @@ class _MyHomePageState extends State { final TextEditingController maxHeightController = TextEditingController(); final TextEditingController qualityController = TextEditingController(); - Future _playVideo(XFile? file) async { + Future _playVideo(int? file) async { if (file != null && mounted) { await _disposeVideoController(); late VideoPlayerController controller; - controller = VideoPlayerController.file(File(file.path)); + controller = VideoPlayerController.fileFd(file); _controller = controller; const double volume = 1.0; - await controller.setVolume(volume); - await controller.initialize(); - await controller.setLooping(true); - await controller.play(); + unawaited(controller.setVolume(volume)); + unawaited(controller.initialize()); + unawaited(controller.setLooping(true)); + unawaited(controller.play()); setState(() {}); } } @@ -106,7 +106,8 @@ class _MyHomePageState extends State { if (file != null && context.mounted) { _showPickedSnackBar(context, [file]); } - await _playVideo(file); + final int fileFd = (_picker as ImagePickerOhos).getFileFd(file?.path); + await _playVideo(fileFd); } else if (isMultiImage) { await _displayPickImageDialog(context, (double? maxWidth, double? maxHeight, int? quality) async { @@ -225,9 +226,16 @@ class _MyHomePageState extends State { textAlign: TextAlign.center, ); } + final Map fileFdlist = (_picker as ImagePickerOhos).fileFdlist; + String path = ''; + for(final String? key in fileFdlist.keys){ + if(fileFdlist[key].toString() == _controller!.dataSource.split('//')[1]){ + path = key!.split('/').last; + } + } return Padding( padding: const EdgeInsets.all(10.0), - child: AspectRatioVideo(_controller), + child: Column(children: [Text(path), AspectRatioVideo(_controller)]) ); } @@ -284,7 +292,7 @@ class _MyHomePageState extends State { Widget _buildInlineVideoPlayer(int index) { final VideoPlayerController controller = - VideoPlayerController.file(File(_mediaFileList![index].path)); + VideoPlayerController.fileFd((_picker as ImagePickerOhos).getFileFd(_mediaFileList![index].path)); const double volume = 1.0; controller.setVolume(volume); controller.initialize(); @@ -309,7 +317,8 @@ class _MyHomePageState extends State { if (response.file != null) { if (response.type == RetrieveType.video) { _isVideo = true; - await _playVideo(response.file); + final int fileFd = (_picker as ImagePickerOhos).getFileFd(response.file?.path); + await _playVideo(fileFd); } else { _isVideo = false; setState(() { diff --git a/packages/image_picker/image_picker_ohos/example/ohos/entry/oh-package.json5 b/packages/image_picker/image_picker_ohos/example/ohos/entry/oh-package.json5 index c3f83b0d3ff3149a1a9b9ead1878b827a44f1086..184fe3a0c86451ea47584e8297f3a8187d70f1c5 100644 --- a/packages/image_picker/image_picker_ohos/example/ohos/entry/oh-package.json5 +++ b/packages/image_picker/image_picker_ohos/example/ohos/entry/oh-package.json5 @@ -23,5 +23,6 @@ '@ohos/flutter_ohos': 'file:../har/flutter.har', '@ohos/integration_test': 'file:./har/integration_test.har', '@ohos/image_picker_ohos': 'file:./har/image_picker_ohos.har', + '@ohos/video_player_ohos': 'file:./har/video_player_ohos.har', }, } \ No newline at end of file diff --git a/packages/image_picker/image_picker_ohos/example/ohos/entry/src/main/module.json5 b/packages/image_picker/image_picker_ohos/example/ohos/entry/src/main/module.json5 index 7bbf78b18f39991b1404061c7437538c7d532bb7..bd577753f4bb08e1417fbab4a10f888e3d9a712d 100644 --- a/packages/image_picker/image_picker_ohos/example/ohos/entry/src/main/module.json5 +++ b/packages/image_picker/image_picker_ohos/example/ohos/entry/src/main/module.json5 @@ -48,6 +48,7 @@ ], "requestPermissions": [ {"name" : "ohos.permission.INTERNET"}, + {"name" : "ohos.permission.READ_MEDIA"} ] } } \ No newline at end of file diff --git a/packages/image_picker/image_picker_ohos/example/pubspec.yaml b/packages/image_picker/image_picker_ohos/example/pubspec.yaml index e06013a158222788c00b61bf0754aecc51537656..b8d675cde7647fcadd4234cc83b0390cc8e11a71 100644 --- a/packages/image_picker/image_picker_ohos/example/pubspec.yaml +++ b/packages/image_picker/image_picker_ohos/example/pubspec.yaml @@ -34,7 +34,10 @@ dependencies: path: ../ image_picker_platform_interface: ^2.8.0 mime: ^1.0.4 - video_player: ^2.1.4 + video_player: + git: + url: "https://gitee.com/openharmony-sig/flutter_packages.git" + path: "packages/video_player/video_player" dev_dependencies: build_runner: ^2.1.10 diff --git a/packages/image_picker/image_picker_ohos/lib/image_picker_ohos.dart b/packages/image_picker/image_picker_ohos/lib/image_picker_ohos.dart index 296a199719bdc37537c3edf8afd8a25171a5e7cb..6c08de09e2a30bb3019cccd9dc3a8c3fa35c80c5 100644 --- a/packages/image_picker/image_picker_ohos/lib/image_picker_ohos.dart +++ b/packages/image_picker/image_picker_ohos/lib/image_picker_ohos.dart @@ -22,6 +22,7 @@ class ImagePickerOhos extends ImagePickerPlatform { : _hostApi = api ?? ImagePickerApi(); final ImagePickerApi _hostApi; + final Map fileFdlist = new Map(); bool useOhosPhotoPicker = false; static void registerWith() { ImagePickerPlatform.instance = ImagePickerOhos(); @@ -67,7 +68,7 @@ class ImagePickerOhos extends ImagePickerPlatform { double? maxWidth, double? maxHeight, int? imageQuality, - }) { + }) async { if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) { throw ArgumentError.value( imageQuality, 'imageQuality', 'must be between 0 and 100'); @@ -81,7 +82,7 @@ class ImagePickerOhos extends ImagePickerPlatform { throw ArgumentError.value(maxHeight, 'maxHeight', 'cannot be negative'); } - return _hostApi.pickImages( + final List path = await _hostApi.pickImages( SourceSpecification(type: SourceType.gallery), ImageSelectionOptions( maxWidth: maxWidth, @@ -90,6 +91,14 @@ class ImagePickerOhos extends ImagePickerPlatform { GeneralOptions( allowMultiple: true, usePhotoPicker: useOhosPhotoPicker), ); + + List pathList = []; + + for(int i = 0 ; i _getImagePath({ @@ -154,9 +163,17 @@ class ImagePickerOhos extends ImagePickerPlatform { usePhotoPicker: useOhosPhotoPicker, ), ); + fileFdlist[paths.first] = int.parse(paths[1] ?? '0'); return paths.isEmpty ? null : paths.first; } + int getFileFd(String? file) { + if(file == null){ + return 0; + } + return fileFdlist[file]!; + } + @override Future getImage({ required ImageSource source, @@ -213,15 +230,20 @@ class ImagePickerOhos extends ImagePickerPlatform { Future> getMedia({ required MediaOptions options, }) async { - return (await _hostApi.pickMedia( + final List paths = await _hostApi.pickMedia( _mediaOptionsToMediaSelectionOptions(options), GeneralOptions( allowMultiple: options.allowMultiple, usePhotoPicker: useOhosPhotoPicker, ), - )) - .map((String? path) => XFile(path!)) - .toList(); + ); + final List pathList = []; + for(int i = 1; i< paths.length; i+=2){ + fileFdlist[paths[i-1]] = int.parse(paths[i] ?? '0'); + pathList.add(paths[i - 1]); + } + + return pathList.map((String? path) => XFile(path!)).toList(); } @override diff --git a/packages/image_picker/image_picker_ohos/ohos/ImagePickerOhos/src/main/ets/image_picker/ImagePickerDelegate.ets b/packages/image_picker/image_picker_ohos/ohos/ImagePickerOhos/src/main/ets/image_picker/ImagePickerDelegate.ets index 12aa195b5d3a27e507ae5f103e530688907ec4f9..cb1d31ed123f16a6339038d2bbc094fead72f7b5 100644 --- a/packages/image_picker/image_picker_ohos/ohos/ImagePickerOhos/src/main/ets/image_picker/ImagePickerDelegate.ets +++ b/packages/image_picker/image_picker_ohos/ohos/ImagePickerOhos/src/main/ets/image_picker/ImagePickerDelegate.ets @@ -265,6 +265,9 @@ export default class ImagePickerDelegate { abilityAccessCtrl.createAtManager() .requestPermissionsFromUser(this.context, ['ohos.permission.READ_MEDIA']) .then(async (permission) => { + if(permission.authResults[0] !== 0){ + return + } let result: ESObject = null; if (this.context) { result = await this.context.startAbilityForResult(want); @@ -483,10 +486,12 @@ export default class ImagePickerDelegate { if (path != null) { let realPath = FileUtils.getPathFromUri(this.context as Context, path); Log.i(ImagePickerDelegate.TAG, "realPath :" + realPath); + let file = fs.openSync(realPath, fs.OpenMode.READ_ONLY); if (realPath != null) { path = realPath; } - pathList.add(path); + pathList.add(realPath) + pathList.add(file.fd.toString()); } let localResult: Result> | null = null; @@ -518,7 +523,14 @@ export default class ImagePickerDelegate { } else if (path) { Log.i(ImagePickerDelegate.TAG, path[0]); - localResult.success(path as ArrayList); + let pathList: ArrayList = new ArrayList(); + for (let i = 0; i < path.length; i++) { + const element: string = path[i]; + let file = fs.openSync(element); + pathList.add(element); + pathList.add(file.fd.toString()); + } + localResult.success(pathList as ArrayList); } }