flutter_cached_network_image icon indicating copy to clipboard operation
flutter_cached_network_image copied to clipboard

FileSystemException: Cannot open file, OS Error: No such file or directory, errno = 2 was thrown

Open Pandian-Natarajan opened this issue 4 years ago β€’ 28 comments

πŸ› Bug Report

In my app, Im having a ListView which shows the users like in contacts app, sometimes its throws FileSystemException: Cannot open file.

════════ Exception caught by image resource service ════════ The following FileSystemException was thrown resolving an image codec:

Cannot open file, path = '/var/mobile/Containers/Data/Application/
B861D1-3A04-498D-AC67-23E00574DD1C/Library/Caches/
libCachedImageData/fdbf1150-a040-11ea-d6f9-4168d77.bin' 
(OS Error: No such file or directory, errno = 2)

When the exception was thrown, this was the stack #0 _File.open. (dart:io/file_impl.dart:366:9) (elided 13 frames from dart:async) Path: /var/mobile/Containers/Data/Application/B861D1-3A04-498D-AC67-23E00574DD1C/Library/Caches/libCachedImageData/fdbf1150-a040-11ea-d6f9-4168d77.bin

Code

ListTile(
      leading: CachedNetworkImage(
          fadeInDuration: Duration(milliseconds: 100),
          fit: BoxFit.cover,
          imageUrl: _filteredUser[index].photoUrl,
          progressIndicatorBuilder: (context, url, downloadProgress) =>
              shimmerImageTile(),
          errorWidget: (context, url, error) => _imagePlaceHolder(),
        ),
    title: userName(),
),

Configuration

Version: cached_network_image: ^2.2.0

Platform:

  • [ 🟒] :iphone: iOS
  • [ 🟒] :robot: Android

Pandian-Natarajan avatar May 27 '20 18:05 Pandian-Natarajan

Same issue if the URL is empty(''). Maybe better to show error placeholder instead of trying to load it?

fryette avatar Jun 05 '20 09:06 fryette

Hi @fryette thanks for your response but Im handling the empty url already and I think this is problem with caching im not sure.

Pandian-Natarajan avatar Jun 05 '20 09:06 Pandian-Natarajan

ya, having same problem... any solution for now?

Soon96 avatar Jun 08 '20 04:06 Soon96

You can find more on this issue here: https://github.com/Baseflow/flutter_cache_manager/issues/135#issuecomment-638753854

renefloor avatar Jun 08 '20 06:06 renefloor

hi sorry for rushing, any status update or solution? now, the images of my app keep disappearing due to the error....

Soon96 avatar Jun 09 '20 08:06 Soon96

@Soon96 No sorry, maybe you can help in finding the real problem and solution. When do the images disappear mostly? Do you load a lot of images, or older images? Do you have any idea how to reproduce this issue?

renefloor avatar Jun 09 '20 08:06 renefloor

When do the images disappear mostly? usually happen in listview, when scroll down the list then scroll up again... some image will missing. Do you load a lot of images, or older images? Quite a number of products images... Do you have any idea how to reproduce this issue? maybe you can test in a list view, scroll up and down....

Error Msg in Debug Console:

I/flutter (27834): Another exception was thrown: FileSystemException: Cannot open file, path = '/data/user/0/ ... /cache/libCachedImageData/ef136f60-aa2d-11ea-ab9b-1d7b0e7aa781.html' (OS Error: No such file or directory, errno = 2)

I/flutter (27834): Another exception was thrown: FileSystemException: Cannot open file, path = '/data/user/0/ ... /cache/libCachedImageData/ef1432b0-aa2d-11ea-c584-d30ca5b9f1c2.html' (OS Error: No such file or directory, errno = 2)

Error video in app: https://www.goldensticks.blog/assets/error_video.mp4

It work perfectly previously, just this few week we start facing this problem.

Soon96 avatar Jun 09 '20 08:06 Soon96

@renefloor is it flutter list view deleting our cache files?

Soon96 avatar Jun 11 '20 03:06 Soon96

The cache manager should only remove files that haven't been used today as given here: https://github.com/Baseflow/flutter_cache_manager/blob/develop/flutter_cache_manager/lib/src/storage/cache_object_provider.dart#L86

So it could be that Android or iOS thinks the cached files are to big and deletes them from the cache.

My guess is that the library handles this better in version 2.3.0-beta.1+1, as there the encoded image is cached in memory and when that is removed from memory the file is downloaded again when the file is removed as well.

But that version has some issues with scrolling if you are on the stable channel. (Fixed in beta and newer)

renefloor avatar Jun 11 '20 12:06 renefloor

thanks for the reply, so for now on we can only wait for the new version stable release or change to other plugin?

Soon96 avatar Jun 12 '20 01:06 Soon96

You could also use the beta version, which is often pretty stable. I also found that there is a 'file.exists()' check missing here: https://github.com/Baseflow/flutter_cache_manager/blob/develop/flutter_cache_manager/lib/src/cache_store.dart#L69, so that could fix part of the problem.

renefloor avatar Jun 12 '20 06:06 renefloor

Using the beta version 2.3.0-beta.1+1 recommended above causes us many of these exceptions

════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown when reporting an error to an image listener:
setState() called after dispose(): _ImageState#679ff(lifecycle state: defunct, not mounted, stream: ImageStream#85a0f(MultiFrameImageStreamCompleter#5ed5d, [750Γ—1061] @ 1.0x, 1 listener), pixels: null, loadingProgress: null, frameNumber: null, wasSynchronouslyLoaded: false)

This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.

And then many of the images don't appear. Hope it will become stable soon to fix this FileSystemException

bradsb avatar Jul 13 '20 11:07 bradsb

@bradsb that is a bug in the Flutter code. The fix is currently available in the beta channel. Hopefully it is soon available in stable. It is also the reason 2.3.0-rc has a minimum flutter version of 1.19.0

renefloor avatar Jul 16 '20 11:07 renefloor

@renefloor I see. Thanks for the reply!

bradsb avatar Jul 22 '20 09:07 bradsb

@renefloor I have changed the dependency code to develop branch, but still got this file exception. Based on the given stack frame, I found the reason why I got file exception is the very first calling of Uri.base.scheme, so I figured it is a flutter issue.

SDK version:

➜  / flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[βœ“] Flutter (Channel stable, v1.12.13+hotfix.5, on Mac OS X 10.15.4 19E287, locale
    zh-Hans-CN)

Exception info: image

ShannonChenCHN avatar Aug 19 '20 03:08 ShannonChenCHN

I'm having the same problem, I got this error but when I try to create a File with a PickedFile path. The code is something like this: File(this.image.path) "image" is a PickedFile object I'm using flutter 1.20.2

DenisBarbosa123 avatar Aug 21 '20 02:08 DenisBarbosa123

@renefloor I have changed the dependency code to develop branch, but still got this file exception. Based on the given stack frame, I found the reason why I got file exception is the very first calling of Uri.base.scheme, so I figured it is a flutter issue.

SDK version:

➜  / flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[βœ“] Flutter (Channel stable, v1.12.13+hotfix.5, on Mac OS X 10.15.4 19E287, locale
    zh-Hans-CN)

Exception info: image

@renefloor We finally found out the real reason now! Because Uri.base try to access current working directory, but there is a case we called -changeCurrentDirectoryPath: in Objective-C to change current directory path to a new path and then we removed that directory, this made current directory path become null. Consequently, calling Uri.base would leads a file system exception.

We fixed this issue and that file system exception doesn't come again.

ShannonChenCHN avatar Aug 31 '20 03:08 ShannonChenCHN

Hi there! Just updated to the 2.3.1 version and the problem with error described by @Soon96 exists on my side as well. Just in my case the lib can't open ...libCachedImageData/<UUID>.json

troyanskiy avatar Sep 02 '20 13:09 troyanskiy

@troyanskiy what's the url? It looks like the url gives a json-file and it makes sense that can't be shown as an image.

renefloor avatar Sep 03 '20 08:09 renefloor

@troyanskiy what's the url? It looks like the url gives a json-file and it makes sense that can't be shown as an image.

@renefloor the backend url is image url, where there are 4 possible returns

  1. Status 200 with image
  2. Status 403 when user does not have rights
  3. Status 401 when no session
  4. Status 404 when the file is not found

So it's always or error code or the image... As far as I understand from reading your code you set file extension based on content-type from the response, so I will double check if the backend has some exceptions and returns JSON instead of image.

Thanks a lot!

troyanskiy avatar Sep 03 '20 13:09 troyanskiy

@renefloor Is it possible to add some extra check somewhere if the file really exists in the cache, instead of throwing the exception?

troyanskiy avatar Sep 03 '20 14:09 troyanskiy

@renefloor I have changed the dependency code to develop branch, but still got this file exception. Based on the given stack frame, I found the reason why I got file exception is the very first calling of Uri.base.scheme, so I figured it is a flutter issue.

SDK version:

➜ / flutter doctor

Doctor summary (to see all details, run flutter doctor -v):

[βœ“] Flutter (Channel stable, v1.12.13+hotfix.5, on Mac OS X 10.15.4 19E287, locale

zh-Hans-CN)

Exception info:

image

@renefloor We finally found out the real reason now! Because Uri.base try to access current working directory, but there is a case we called -changeCurrentDirectoryPath: in Objective-C to change current directory path to a new path and then we removed that directory, this made current directory path become null. Consequently, calling Uri.base would leads a file system exception.

We fixed this issue and that file system exception doesn't come again.

ε“₯δ»¬οΌŒζ€ŽδΉˆζ”Ήηš„οΌŸθ―·ζ•™δΈ‹

wochen85 avatar Sep 24 '20 00:09 wochen85

@renefloor I have changed the dependency code to develop branch, but still got this file exception. Based on the given stack frame, I found the reason why I got file exception is the very first calling of Uri.base.scheme, so I figured it is a flutter issue. SDK version:

➜  / flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[βœ“] Flutter (Channel stable, v1.12.13+hotfix.5, on Mac OS X 10.15.4 19E287, locale
    zh-Hans-CN)

Exception info: image

@renefloor We finally found out the real reason now! Because Uri.base try to access current working directory, but there is a case we called -changeCurrentDirectoryPath: in Objective-C to change current directory path to a new path and then we removed that directory, this made current directory path become null. Consequently, calling Uri.base would leads a file system exception.

We fixed this issue and that file system exception doesn't come again.

@renefloor ,hello,is that problem?

wochen85 avatar Sep 30 '20 02:09 wochen85

Im my case, it is calculating something on memory and when app restarted it works. So below is my workaround solution

class CustomCachedImage extends StatefulWidget {
  final String imageUrl;
  final double radius = 20.0;

  CustomCachedImage({
    @required this.imageUrl,
  });

  @override
  _State createState() => _State();
}

class _State extends State<CustomCachedImage> {
  @override
  Widget build(BuildContext context) {
    return CachedNetworkImage(
      imageUrl: widget.imageUrl,
      imageBuilder: (context, imageProvider) {
        return Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(widget.radius)),
            image: DecorationImage(
              image: imageProvider,
              alignment: Alignment.topCenter,
              fit: BoxFit.cover,
              scale: 1,
            ),
          ),
        );
      },
      placeholder: (BuildContext context, String url) => Container(),
      // If error happens load image by using NetworkImage 
      errorWidget: (context, url, error) {
        return Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(widget.radius)),
            image: DecorationImage(
              image: NetworkImage(widget.imageUrl),
              alignment: Alignment.center,
              fit: BoxFit.cover,
              scale: 1,
            ),
          ),
        );
      },
    );
  }
}

talhakosen avatar Dec 17 '20 12:12 talhakosen

what is the solution for this when im using image picker image

noor848 avatar Jan 19 '22 22:01 noor848

same Issue as @noor848 when using Image Picker

SelimSayeh avatar Mar 28 '22 20:03 SelimSayeh

what is the solution for this when im using image picker image

same issue, what is the solution

DenisMrGnusa avatar Jun 21 '22 13:06 DenisMrGnusa

We are still facing issue. Is there any update on this ?

asiteandroid avatar Jan 31 '24 08:01 asiteandroid