flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
166.2k stars 27.49k forks source link

[image_picker] The file returned by ImagePicker.pickVideo doesn't exist on IOS #29670

Closed arkivika closed 3 years ago

arkivika commented 5 years ago

Calling file.exists() on the file returned by the ImagePicker.pickVideo() returns false.

flutter doctor -v ```bash > [✓] Flutter (Channel dev, v1.3.13, on Mac OS X 10.14.3 18D109, locale en-FI) > • Flutter version 1.3.13 at /Users/arkivika/Development/flutter > • Framework revision 59ce7d6bff (2 days ago), 2019-03-18 11:20:10 -0700 > • Engine revision e75f6de01f > • Dart version 2.2.1 (build 2.2.1-dev.1.0 None) > > [✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3) > • Android SDK at /Users/arkivika/Library/Android/sdk > • Android NDK location not configured (optional; useful for native profiling support) > • Platform android-28, build-tools 28.0.3 > • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java > • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01) > • All Android licenses accepted. > > [✓] iOS toolchain - develop for iOS devices (Xcode 10.1) > • Xcode at /Applications/Xcode.app/Contents/Developer > • Xcode 10.1, Build version 10B61 > • ios-deploy 1.9.4 > • CocoaPods version 1.5.3 > > [✓] Android Studio (version 3.3) > • Android Studio at /Applications/Android Studio.app/Contents > • Flutter plugin version 33.3.1 > • Dart plugin version 182.5215 > • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01) > > [✓] VS Code (version 1.32.2) > • VS Code at /Applications/Visual Studio Code.app/Contents > • Flutter extension version 2.24.0 > > [✓] Connected device (1 available) > • iPhone 6 Plus (-) • c3e954267f3f2d620b9fe9e95a455fb30aaf651f • ios • iOS 12.1.4 > > • No issues found! ```
prashanthbmcpg commented 5 years ago

Is there any solution for this issue or should I move on with a new plugin ?

markqq commented 5 years ago

I think ImagePicker.pickVideo is adding a redundant prefix “file://” at the beginning of the file path. The code below could solve the problem.

File file = await ImagePicker.pickVideo(source: ImageSource.gallery);
String filePath = file.path;
if (filePath.indexOf('file://') == 0)
  filePath = filePath.split('file://')[1];
file = File(filePath);
escamoteur commented 5 years ago

Hi, could you please confirm if this problem still exists with the latest versions on stable or master?

Thanks a lot

macmatrix commented 5 years ago

I have the same exact problem and only with iOS (android is ok). I tried the fix proposed by @markqq but it does not work. ImagePicker.pickVideo(source: ImageSource.gallery) does not generate a file. For reference the ImagePicker.pickVideo(source: ImageSource.camera) works fine in iOS and Android, so the problem is only with picking video from iOS gallery.

Here is my code and the error log

_pickVideoPlus() async {
    File file = await ImagePicker.pickVideo(source: ImageSource.gallery);
    print('file.path =  ' + file.toString());
    String filePath = file.path;
    print('file.path =  ' + filePath);
    if (filePath.indexOf('file://') == 0)
      filePath = filePath.split('file://')[1];
    print('file.path dopo=  ' + filePath);
    final sizeFile = File(filePath).lengthSync();

    print('file.path after =  ' + filePath);

    if (sizeFile < maxVideoSIzeInBytes) {
      _video = File(filePath);
      print('printing... size video 1 = ' + _video.lengthSync().toString());
      print('printing... size video 2 = ' +
          File(_video.resolveSymbolicLinksSync()).lengthSync().toString());

      print("exist sync da pick video? " + _video.existsSync().toString());

      _videoPlayerController = VideoPlayerController.file(_video)
        ..initialize().then((_) {
          setState(() {});
          _videoPlayerController.play();
        });
    } else {
      showDialog(
          context: context,
          builder: (BuildContext context) => ShowDialogToDismiss(
              title: 'Error',
              content: 'The size of the video is too big. Your video was ' +
                  (sizeFile ~/ 1048576).toString() +
                  ' MB.Please upload a video which is less than ' +
                  maxVideoSizeInMegaBytes.toString() +
                  ' MB in size',
              buttonText: 'CLOSE'));
    }
  }

`2019-10-22 09:02:54.821538+0100 Runner[2166:552419] [core] "Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)"" 2019-10-22 09:02:54.830873+0100 Runner[2166:552338] flutter: file.path = File: '/private/var/mobile/Containers/Data/PluginKitPlugin/C2F6988A-77EE-470A-B1E9-643F834147A5/tmp/trim.81C21145-578E-418F-9187-59EFA15DA124.MOV' 2019-10-22 09:02:54.830934+0100 Runner[2166:552338] flutter: file.path = /private/var/mobile/Containers/Data/PluginKitPlugin/C2F6988A-77EE-470A-B1E9-643F834147A5/tmp/trim.81C21145-578E-418F-9187-59EFA15DA124.MOV 2019-10-22 09:02:54.830990+0100 Runner[2166:552338] flutter: file.path dopo= /private/var/mobile/Containers/Data/PluginKitPlugin/C2F6988A-77EE-470A-B1E9-643F834147A5/tmp/trim.81C21145-578E-418F-9187-59EFA15DA124.MOV 2019-10-22 09:02:54.835093+0100 Runner[2166:552338] [VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: FileSystemException: Cannot retrieve length of file, path = '/private/var/mobile/Containers/Data/PluginKitPlugin/C2F6988A-77EE-470A-B1E9-643F834147A5/tmp/trim.81C21145-578E-418F-9187-59EFA15DA124.MOV' (OS Error: Operation not permitted, errno = 1)

0 _File.throwIfError (dart:io/file_impl.dart:643:7)

1 _File.lengthSync (dart:io/file_impl.dart:385:5)

2 _EditVideoState._pickVideoPlus (package:cc/screens/account/backend/03_edit_video.dart:57:37)

#3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:654:14) #4 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:729:32) #5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24) #6 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11) #7 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7) #8 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9) #9 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13) #10 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11) #11 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19) #12 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22) #13 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7) #14 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7) #15 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7) #16 _rootRunUnary (dart:async/zone.dart:1136:13) #17 _CustomZone.runUnary (dart:async/zone.dart:1029:19) #18 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7) #19 _invoke1 (dart:ui/hooks.dart:263:10) #20 _dispatchPointerDataPacket (dart:ui/hooks.dart:172:5)` Any suggestion???
GuillaumeJulien commented 5 years ago

I found this, if than can help you https://github.com/flutter/flutter/issues/41046

macmatrix commented 5 years ago

Manu thanks for this. I read through the links but still not sure what to do. Is the plan that I should wait until the next version of the image_picker plugin?

escamoteur commented 5 years ago

Hav you tried this here to get the coming PR already now? https://github.com/flutter/flutter/issues/41046#issuecomment-538310691

macmatrix commented 5 years ago

Hi, I have not tried. I am pretty a beginner... I guess I need to put it in the pubspec.yaml file but where should I write it? here is my file

pubspec.yaml ```yaml name: xxx description: xxx version: 1.0.0+2 environment: sdk: ">=2.0.0-dev.68.0 <3.0.0" dependencies: flutter: sdk: flutter cupertino_icons: ^0.1.2 animated_text_kit: ^1.3.1 firebase_core: ^0.4.0+8 firebase_auth: ^0.14.0+2 cloud_firestore: ^0.12.9+5 modal_progress_hud: ^0.1.3 cached_network_image: ^1.1.1 url_launcher: ^5.1.2 geolocator: ^5.1.3 video_player: ^0.10.2+1 qr_flutter: ^3.0.1 shared_preferences: ^0.5.3+4 google_maps_flutter: ^0.5.21+3 barcode_scan: ^1.0.0 flutter_email_sender: ^2.0.3 badges: ^1.1.0 geoflutterfire: ^2.0.3+5 provider: ^3.1.0 percent_indicator: ^2.1.1+1 stripe_native: ^1.1.2 firebase_database: ^3.0.7 flutter_launcher_icons: ^0.7.3 firebase_messaging: ^5.1.6 intro_slider: ^2.2.8 firebase_storage: ^3.0.6 image_picker: ^0.6.1+8 flutter_icons: ios: true android: true image_path_ios: "assets/icons/icon.png" image_path_android: "assets/icons/icon.png" adaptive_icon_background: "assets/icons/background.png" adaptive_icon_foreground: "assets/icons/foreground.png" dev_dependencies: flutter_test: sdk: flutter flutter: uses-material-design: true assets: - assets/images/ fonts: - family: Pacifico fonts: - asset: fonts/Pacifico-Regular.ttf - family: Open Sans fonts: - asset: fonts/OpenSans-Regular.ttf - family: Montserrat fonts: - asset: fonts/Montserrat-Regular.ttf ```
escamoteur commented 5 years ago

Just replace the line with image_pickerwith

image_picker:
    git:
      url: git://github.com/miguelpruivo/plugins.git
      path: packages/image_picker
      ref: image_picker-Fix-#41046
macmatrix commented 5 years ago

I get this error (sorry I feel a bit noob...)

Git error. Command: git clone --mirror git@github.com:miguelpruivo/plugins.git /Users/xxx/Developer/flutter/.pub-cache/git/cache/plugins-xxx Cloning into bare repository '/Users/xxx/Developer/flutter/.pub-cache/git/cache/plugins-xxx'... Host key verification failed. fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

escamoteur commented 5 years ago

Sorry, my fault I correct the above pubspec.

escamoteur commented 5 years ago

I think that should work now. Please also see https://flutter.dev/docs/development/packages-and-plugins/using-packages

macmatrix commented 5 years ago

Many thanks, it all works perfectly !

iassal commented 4 years ago

It seems like this issue is already resolved based on the discussion. Let's close it

TahaTesser commented 3 years ago

Hi @arkivika This is correctly working with the latest image_picker version 0.6.7+22, it is getVideo now instead of pickVideo which returns the PickedFile object. PickedFile doesn't have .exist properly but it has .path property which verified file does exist after picking on iOS

Code modification in the official example

  void _onImageButtonPressed(ImageSource source, {BuildContext context}) async {
    if (_controller != null) {
      await _controller.setVolume(0.0);
    }
    if (isVideo) {
      final PickedFile file = await _picker.getVideo(
          source: source, maxDuration: const Duration(seconds: 10));

      print("File exist? : ${file.path}");

logs

[+18429 ms] flutter: File exist? :
/Users/tahatesser/Library/Developer/CoreSimulator/Devices/959C5334-5E00-420C-BF30-033E4F51150A/data/Containers/Data/Application/B462
AAC1-1913-4263-A7C3-C7549025DA52/tmp/trim.8E09A695-1CB8-4A44-BF03-3AC2D0BD09FB.MOV

Closing as fixed. If you disagree, please write in the comments and I will reopen it. Thank you

github-actions[bot] commented 3 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.