skip to Main Content
File? imageFile;
ImagePicker imagePicker = ImagePicker();

Future<File?> getImage(ImageSource imageSource) async {
final XFile? pickedFile = await imagePicker.pickImage(source: imageSource);
if (pickedFile == null) {
return null;
}
return File(pickedFile.path);
}
// source is camera

// save to SharedPreferences
final String duplicateFilePath = await getApplicationDocumentsDirectory().then((value) => value.path);
final fileName = basename(file.path);
final File newImage = await file.copy('$duplicateFilePath/$fileName');

// save newImage.path

After restarting the app, when I try to load the image, I encounter the following error:
The following PathNotFoundException was thrown resolving an image codec:
Cannot retrieve length of file, path = ‘/var/mobile/Containers/Data/Application/7D7645C5-CAF6-43F5-A484-CA93872D2996/Documents/image_picker_46D51663-C51D-4DA6-B350-57C79EFAC368-3158-00000333318B1CCD.jpg’ (OS Error: No such file or directory, errno = 2)

What could be the problem? this issue only ios not android

2

Answers


  1. It seems like the issue might be related to the path you are using to save and load the image. When you restart the app, the previously saved file might not be found at the same location. Here are a few suggestions to fix this issue:

    1. Use a Stable Directory Path: Instead of relying on the exact path which might change on app restart, you can use a stable directory like getApplicationDocumentsDirectory() to save and load images. This ensures that the images will be accessible even after app restart.

    2. Check File Existence: Before trying to load the image, ensure that the file actually exists at the specified path. If the file does not exist, handle it gracefully, for example, by displaying a placeholder image or prompting the user to select a new image.

    Here’s how you can modify your code to use the application documents directory and check file existence before loading the image:

    import 'package:path_provider/path_provider.dart';
    
    Future<String> getImagePath() async {
      final directory = await getApplicationDocumentsDirectory();
      return directory.path;
    }
    
    // Function to save image
    Future<void> saveImage(File file) async {
      final String imagePath = await getImagePath();
      final fileName = basename(file.path);
      final File newImage = await file.copy('$imagePath/$fileName');
    
      // Save newImage.path to SharedPreferences
    }
    
    // Function to load image
    Future<String?> loadImagePath() async {
      final String imagePath = await getImagePath();
      // Load image path from SharedPreferences
    
      final String? savedImagePath = ''; // Load saved image path from SharedPreferences
      final File imageFile = File('$imagePath/$savedImagePath');
      if (await imageFile.exists()) {
        return imageFile.path;
      } else {
        return null; // Handle file not found gracefully
      }
    }
    

    With this approach, you ensure that the image is saved and loaded from a stable directory location, and you handle the scenario where the file might not exist gracefully.

    Login or Signup to reply.
  2. It is not a bug, it is a feature (from the Apple point of view)

    This value 7D7645C5-CAF6-43F5-A484-CA93872D2996 is a unique hash for the iOS app installation.

    Restarting the app from the IDE is a reinstallation. So the value changes. That’s why the app cannot find the file location.

    If you install from the TestFlight or the App Store the same app, then close and open the app again, the file will be in place. After deleting the app and installing again, will not.

    Good luck!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search