I am trying to change the user avatar, I am using image_picker package to get an image file, the I am copying the image to app folder, after that I set the state to see the image that was picked. I get some exception error:
Exception caught by image resource service
Unable to load asset: /data/user/0/com.ufscar.app_mental/app_flutter/profile-picked-user.png
This is my code, when the user click at the circle avatar this function begin:
AssetImage image = AssetImage('assets/images/profile-user.png');
pickNewImage() async {
final status = await Permission.storage.status;
String folderPath = "";
if (status != PermissionStatus.granted) {
await Permission.storage.request();
}
if (await Permission.storage.status.isGranted) {
Directory folderDir = await getApplicationDocumentsDirectory();
folderPath = folderDir.path;
}
final picker = ImagePicker();
final pickedImage = await picker.pickImage(source: ImageSource.gallery);
if (pickedImage != null) {
File pickedImageFile = File(pickedImage.path);
await pickedImageFile.copy("$folderPath/profile-picked-user.png");
setState(() {
image = AssetImage('$folderPath/profile-picked-user.png');
});
} else {
return;
}
}
The image is selected right and it goes to the right folder(/data/user/0/com.ufscar.app_mental/app_flutter/profile-picked-user.png), I can see it with android studio. The image is not corrupted.
Anyone can give me a tip?
As the error message already says, it cannot load this asset, because AssetImage fetches an Image from the AssetBundle, which is defined in your pubspec.yaml
To load an image from the storage use the Image.file constructor:
Image.file(File(path))