steveOS
steveOS

Reputation: 133

Unable to load asset Flutter

When I try to load image selected from gallery to be displayed on my app on first run, it comes up with this error.

======== Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: /data/user/0/com.example.app_test/cache/image_picker5494866148665379741.jpg

When the exception was thrown, this was the stack: 
#0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:225:7)
<asynchronous suspension>
#1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:668:31)
#2      AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:651:14)
#3      ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:504:13)
...
Image provider: ExactAssetImage(name: "/data/user/0/com.example.app_test/cache/image_picker5494866148665379741.jpg", scale: 1.0, bundle: null)
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#d8a39(), name: "/data/user/0/com.example.versus_pay_client/cache/image_picker5494866148665379741.jpg", scale: 1.0)
====================================================================================================

When I hot reload the app the image gets displayed.

Future getImage() async {
pickedFile = await picker.getImage(source: ImageSource.gallery);

setState(() {
  if (pickedFile != null) {
    image = File(pickedFile.path);
  } else {
    print('No image selected.');
  }
});
new Container(
   height: 120.0,
   width: 120.0,
   decoration: new BoxDecoration(
      image: new DecorationImage(
      image: new ExactAssetImage(image.path),
      fit: BoxFit.cover,
   ),
   borderRadius: new BorderRadius.all(
      const Radius.circular(90.0)),
     ),
   ),

What am I doing wrong?

Upvotes: 1

Views: 1544

Answers (1)

Ayyaz Shair
Ayyaz Shair

Reputation: 620

I'd run into same problem, this is because the new version of image_picker picks the file from camera/gallery and then writes it to a temporary storage in the root of the application, It means the file is not an asset. You can use FileImage instead, like this:

new Container(
  height: 120.0,
  width: 120.0,
  decoration: new BoxDecoration(
    image: new DecorationImage(
      image: new FileImage(image),
      fit: BoxFit.cover,
    ),
    borderRadius: new BorderRadius.all(const Radius.circular(90.0)),
  ),
);

Upvotes: 5

Related Questions