poultrynews
poultrynews

Reputation: 659

'Cannot open file' after cropping image

The goal is to:

1) Allow the user to choose a picture [I use image_picker for that]

2) User crops image to a 1:1 aspect ratio [I use image_crop for that]

3) Upload image to Python backend

The problem:

After cropping image, attempting to read the image to Post it returns:

Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.example.AppName/cache/image_crop_26d4daef-e297-456c-9c6d-85d2e4c0d0662042323543401355956.jpg' (OS Error: No such file or directory, errno = 2)

The weird part is, I can display the image just fine within Flutter using `FileImage(_imageFile)' (considering _imageFile is the File variable)

It's just that I cannot use _imageFile.length() or even base64Encode(_imageFile.readAsBytesSync()).

Any ideas on what's happening and how to fix it?

Upvotes: 0

Views: 529

Answers (1)

Krishna Vyas
Krishna Vyas

Reputation: 1028

path_provider plugin supports access to two filesystem locations:

  1. Temporary directory,
  2. Documents directory.

    — The temporary directory is the cache and its content may be erased by the system at any time. So, storing data here is not good for us to get it later.

    — The documents directory is the one we to choose now, this is a directory for the app to store files that only it can access

And You are using temporary directory (/data/user/0/com.example.AppName/cache/image_crop_26d4daef-e297-456c-9c6d-85d2e4c0d0662042323543401355956.jpg)

And if file is not erased then you can use following snippet to read file content:

final directory = await getTemporaryDirectory();
// For your reference print the AppDoc directory 
final path = directory.path;
final file = File('$path/data.txt');
String contents = await file.readAsString();
return contents;

Upvotes: 1

Related Questions