无法加载资产 Flutter

时间:2021-02-05 16:55:00

标签: android flutter android-studio

当我尝试加载从图库中选择的图像以在第一次运行时显示在我的应用程序上时,出现此错误。

======== 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)
====================================================================================================

当我热重新加载应用程序时,图像就会显示出来。

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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,这是因为新版本的 image_picker 从相机/图库中选择文件,然后将其写入应用程序根目录中的临时存储,这意味着该文件不是资产。您可以改用 FileImage,如下所示:

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