无法打开文件,path ='file:/// path'(操作系统错误:没有这样的文件或目录,错误= 2)

时间:2017-10-17 04:33:10

标签: dart flutter

您好我正在尝试使用图片库减少从图库中选取的图像尺寸 在尝试时,我面临着像这样的错误

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)

可以任何一个帮助,我的代码看起来像这样

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);

2 个答案:

答案 0 :(得分:0)

问题不在于使用图像选择器读取文件或转换为较小的文件,而是将文件写入文件系统。我接受了,你拿了dart image package的示例代码 在调整大小后将文件写入文件系统。这种形式在智能手机上是不可能的。

您发布的代码实际上并未导致错误,但此行确实:

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

这个问题是imagePicker隐藏了图像的真实uri。它会为您提供如下所示的路径:.../tmp/image_picker_1547A964-791E-426F-A682-33D3AFA995BA-18295-0003861F9255294A.jpg,它不能用于保存新缩略图。

您可以尝试避免首先保存文件或将其保存到实际存在的位置(例如,您可以使用path_provider plugin)。

答案 1 :(得分:0)

我认为问题是将Image URI作为String传递。 有一个fromUri构造函数,您可以在其中传递文件URI。

尝试使用

    new Io.File.fromUri(imageFile.uri)

而不是

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