Salah Rashad
Salah Rashad

Reputation: 468

"Unable to load asset" with image picker

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;    
    });    
  });    
}

Upvotes: 5

Views: 7973

Answers (3)

Jhon BN
Jhon BN

Reputation: 11

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,
        );
      }

Upvotes: 1

anoop benzier
anoop benzier

Reputation: 11

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

Upvotes: 1

Spatz
Spatz

Reputation: 20118

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.

Upvotes: 18

Related Questions