I'm using Image Picker package in my Flutter project
I choose image from gallery then preview it in Image.asset widget
The problem here is if image name "example_name.png" (without spaces) the image is visible on the screen, but if image name "example name.png" (with spaces) the image is invisible like this Screenshot.
Error: Unable to load asset: /storage/emulated/0/Download/images (9).jpeg
File _image;
Image.asset(
_image != null
? "${_image.path}"
: getImage("icon.png"),
fit: BoxFit.cover,
width: 120,
height: 120,
);
...
Future chooseFile() async {
await ImagePicker.pickImage(source: ImageSource.gallery).then((image) {
setState(() {
_image = image;
});
});
}
You are using the wrong Image constructor. Use Image.file instead of Image.asset. Image.asset load files packaged in the application (assets section of pubspec.yaml) and ImagePicker does not have access to them.
in the image_picker (version 0.6.7 + 22) I was able to recover the image with this condition
if (photo == null) {
return Image (
image: AssetImage ('assets / no-image.png'),
height: 300.0,
fit: BoxFit.cover,
);
} else {
return Image.file (
Photo,
height: 300.0,
fit: BoxFit.cover,
);
}
Using Image.file is a good option but you like to display it in an effective way use
Image.file(_image).image this will help you to convert Image file to image provider
Related
after turning off the internet i gets this error: "Remove network images from cache on any exception during loading"
my app fetch pictures from api. how can i fix this bug how i prevent that?
return Column(children: [
GestureDetector(
child: Image.network(thumbnailUrl))])
You can use the Cached Network Image Plugin if you are developing for android and ios. In Web, the plugin is not working as expected.
Use the following as a widget for your application.
import 'package:cached_network_image/cached_network_image.dart';
Widget commonCacheImageWidget(String? url, {double? width, BoxFit? fit, double? height}) {
if (url!.startsWith('http')) {
return CachedNetworkImage(
placeholder: (context, url) => Image.asset('assets/path....', fit: BoxFit.cover),
imageUrl: url,
height: height,
width: width,
fit: fit,
);
} else {
return Image.asset(url, height: height, width: width, fit: fit);
}
}
In Placeholder you can have any widget, here am using a local image that is stored as an asset.
Or You can check whether the internet connection is there or not before loading the image and change that to some other widget.
The user picks an image (resulting in PickedFile - a class that is supported on web), and I want to present that image on the screen (another screen).
For Android and iOS it's very simple:
Image.file(
File(widget.pickedFile.path),
fit: BoxFit.scaleDown,
)
(see also How to Save set image of PickedFile type to a image in Flutter?).
However, for Web, the class File isn't supported.
How do I present the user with an Image on Web?
Without trying it personally, I suspect that PickedFile is a list of bytes.
Image has another constructor Image.memory() so my first suggestion is:
Uint8List pickedFileBytes = await widget.pickedFile.readAsBytes();
Image.memory(pickedFileBytes, fit: BoxFit.scaleDown);
To display pickedFile (local storage images) on Flutter Web use:
Image.network(file.path)
Hope this works for you :-)
file_picker: file.path not work in web becuase path always null.
solution: use file.bytes instead.
so try this solution worked for me :
Image.memory(
controller.file!.bytes!,
fit: BoxFit.fill,
height: 270,
width: Get.width,
)
I build an user profile page where you may change the profile image. The default picture is in assets. The selected picture, either from gallery or camera, is stored localy. All this works fin. My code to display the profile picture looks like:
Container(
width: 140.0,
height: 140.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
image: new FileImage(File(_image.path)),
//image: new ExactAssetImage(_image.path),
fit: BoxFit.cover,
),
))
The FileImage shows the local image correctly. The ExactAssetImage shows the default asset image correctly. My question: how to check if local image exists and display it or use the default assset image?
Update:
I initialize my _image like:
File _image = File('assets/images/user_profile_static.png');
Once I have chosen the new image I save it localy using following code:
void _imgFromGallery() async {
final imageFile = await ImagePicker().getImage(source: ImageSource.gallery);
if (imageFile == null) return;
Directory directory = await getApplicationDocumentsDirectory();
File tmpFile = File(imageFile.path);
tmpFile = await tmpFile.copy('${directory.path}/my_profile.jpg');
setState(() {
_image = tmpFile;
});
}
So on next visit of the page, if my_profile.jpg exists, show it , else show default image.
I think this should work
Image(image: FileImage(image)??AssetImage(assetName),),
Im using Image.Network to display a image based on a URL :
ClipOval(
child: Image.network(
'http://myurl${userId}.png',
width: 100,
height: 100,
fit: BoxFit.cover,
key: profileImageKey,
),
),
I'm trying get the user to upload a new profile image, however, i'm retaining the same filename (userid + .png). Once the user has uploaded the image the above is display is not displaying the new image.
I've tried rebuilding the widget using :
setState(() {
//generateKey();
profileImageKey = ValueKey(new Random().nextInt(100));
userId = userId;
});
I've also tried removing the url from the cache using :
PaintingBinding.instance.imageCache.evict('http://myurl${userId}.png');
However, none of these methods work. Is there a way to update the Image.Network to download and use the new image that was uploaded ?
Thanks
Based on #pskink's comment https://stackoverflow.com/a/60916852/2252830
The solution to this question was to use the evict method on Image. So I refactored my code to below :
Image profileImage;
...
profileImage = Image.network(
'http://myurl${userId}.png',
width: 100,
height: 100,
fit: BoxFit.cover,
key: profileImageKey,
);
...
ClipOval(
child: profileImage,
),
...
profileImage.image.evict();
setState(() {
//generateKey();
profileImageKey = ValueKey(new Random().nextInt(100));
userId = userId;
});
I'm working with saved image from Url with Gallery Saver. Everything it's oke , i can insert image from URL, but i don't know how to fetch image from picture Directory .
It's different about getApplicationDocumentsDirectory() or getExternalStorageDirectory() ?
I want to display the image that was just saved.
Any solution ?
Save image Code :
void _testSaveImage() async {
String path = "${Urls.BASE_API_IMAGE}/berita/${widget.gambarBerita}";
GallerySaver.saveImage(path).then((bool success) {
print('Success add image $path');
}).catchError((onError) {
print('Error add image $path');
});
}
Location Image saved
Then just return the path you have and use a Image.file() widget to display it, lets say you have a Column():
Column(children: <Widget>[
[...]
Text('Here is my image:'),
if (path.isNotEmpty && path != null)
SizedBox(
height: 200,
width: 200,
child: Image.file(File(path),
),
Text('Caption of the image'),
[...]
),
Docs for Image class here
Load local saved images using path and use it
var file = new File('path');
Image.file(file )