Cannot open file, path = 'file:///path' (O

2019-07-18 19:21发布

问题:

Hi I am trying reduce image size picked from gallery using image library while trying that I am facing error like this

E/flutter (13796): [ERROR:lib/tonic/logging/dart_error.cc(16)]    Unhandled exception:
E/flutter (13796): FileSystemException: Cannot open file, path = 'file:///storage/emulated/0/WhatsApp/Media/WhatsApp%20Images/IMG-20171016-WA0020.jpg' (OS Error: No such file or directory, errno = 2)
E/flutter (13796): #0      _File.throwIfError (file_impl.dart:618)
E/flutter (13796): #1      _File.openSync (file_impl.dart:473)
E/flutter (13796): #2      _File.readAsBytesSync (file_impl.dart:533)
E/flutter (13796): #3      _AddNewUserState.qqq (/data/data/com.prayuta.chitfunds/cache/chitfundsUNFPRG/chitfunds/lib/users.dart:420:97)
E/flutter (13796): <asynchronous suspension>
E/flutter (13796): #4      _AddNewUserState.build.<anonymous closure> (/data/data/com.prayuta.chitfunds/cache/chitfundsUNFPRG/chitfunds/lib/users.dart:686:19)
E/flutter (13796): #5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter (13796): #6      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:149:9)
E/flutter (13796): #7      TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:119:7)
E/flutter (13796): #8      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (13796): #9          BindingBase&SchedulerBinding&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:147:20)
E/flutter (13796): #10     BindingBase&SchedulerBinding&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
E/flutter (13796): #11     BindingBase&SchedulerBinding&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
E/flutter (13796): #12     BindingBase&SchedulerBinding&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
E/flutter (13796): #13     BindingBase&SchedulerBinding&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
E/flutter (13796): #14     _invoke1 (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:100)
   E/flutter (13796): #15     _dispatchPointerDataPacket (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:58)

can any one help and my code look like this

import 'package:image/image.dart' as convertinglib;
File imageFile = await ImagePicker.pickImage();
convertinglib.Image image = convertinglib.decodeImage(new Io.File(imageFile.uri.toString()).readAsBytesSync());
convertinglib.Image thumbnail = convertinglib.copyResize(image, 120);

回答1:

I suppose the issue is passing the Image URI as a String. There is a fromUri constructor where you can pass the file URI.

Try using

    new Io.File.fromUri(imageFile.uri)

instead of

    new Io.File(imageFile.uri.toString())


回答2:

The problem is not in reading the file with the image picker or with the conversion to a smaller size but with the writing of the file to the file system. I take it, you took the example code of the dart image package which writes the file to the file system after resizing it. This is in this form not possible on smartphones.

The code you posted didn't actually cause the error but this line did:

new Io.File(imageFile.uri.toString())
   ..writeAsBytesSync(convertingLib.encodePng(thumbnail))

The problem with this is that the imagePicker hides the real uri of the image. It gives you a path that looks something like this: .../tmp/image_picker_1547A964-791E-426F-A682-33D3AFA995BA-18295-0003861F9255294A.jpg which can't be used to save your new thumbnail.

You can either try to avoid saving the file in the first place or save it to a location that actually exists (you could use for example the path_provider plugin for that purpose).



标签: dart flutter