I'm trying to let the user choose an image, copy it to the internal app folder, and then display it back. I'm getting an error
Unable to load asset: /data/user/0/com.my.app/app_flutter/IMG-20230923-WA0001.png
Exception: Asset not found
void _getImage() async {
// Pick an image from the user gallery
ImagePicker imagePicker = ImagePicker();
final imageFile = await imagePicker.pickImage(source: ImageSource.gallery);
if (imageFile != null) {
// Get the application directory
final String appDirPath = (await getApplicationDocumentsDirectory()).path;
setState(() {
int fileNameIndex = imageFile.path.lastIndexOf("/");
cardBackgroundPath =
"$appDirPath${imageFile.path.substring(fileNameIndex)}";
});
File imageFileTemp = File(imageFile.path);
try {
// Copy the source file to the destination
await imageFileTemp.copy(cardBackgroundPath);
// Check if the file was successfully copied
if (File(cardBackgroundPath).existsSync()) {
debugPrint('File copied to: $cardBackgroundPath');
} else {
debugPrint('Failed to copy the file.');
}
} catch (e) {
// Handle any errors that may occur during the copy operation
debugPrint('Error copying the file: $e');
}
// imageFile.saveTo(cardBackgroundPath);
debugPrint("CardbackgroundPath: $cardBackgroundPath");
}
- I have verified that the file exists in the right path.
- I also get the below debugPrint output:
I/flutter (17123): File copied to: /data/user/0/com.example.ritual/app_flutter/IMG-20230923-WA0001.jpg - Static loading of images are working fine, that is images put into the asset folder and loaded while coding the application, and adding those filepaths into the
pubspec.yamlfile - I am using Image.asset(, fit:BoxFot.cover, <width & height>), attributes to display the image
Assets in pubspec.yaml
# To add assets to your application, add an assets section, like this:
assets:
- assets/
- assets/icons/
- assets/fonts/
- assets/images/