无法在Flutter中使用image_picker从图库中加载图像失败

问题描述 投票:0回答:1

我是Flutter的新手,我编写了一个简单的应用程序,可以打开图库,以便用户选择图片,但是在emulatorphysical device上我都遇到相同的错误:

════════图像资源服务caught捕获的异常════════

引发以下断言以解析图像编解码器:无法加载资产:/data/user/0/com.example.upload_image_example/cache/image_picker915145764706640017.jpg

[图像提供者:AssetImage(bundle:null,名称:“ /data/user/0/com.example.upload_image_example/cache/image_picker915145764706640017.jpg”)图片密钥:AssetBundleImageKey(捆绑包:PlatformAssetBundle#c486d(),名称:“ /data/user/0/com.example.upload_image_example/cache/image_picker915145764706640017.jpg”,比例:1.0)

我已经检查了路径,照片确实存在于该路径中。

我已经更新了pubspec.yaml,以使用资产目录中的其他图像。但是问题出在我用图像选择器选择照片时:

var photo = await ImagePicker.pickImage(source: ImageSource.gallery);
  setState(() {
      imageFile = photo;
    });
Widget _ImageView() {
    if (imageFile == null) {
      return CircleAvatar(
        radius: 80.0,
        backgroundImage: AssetImage('assets/images/avatar_blank.jpeg'),
      );
    } else {
      return CircleAvatar(
        radius: 80.0,
        backgroundImage: AssetImage(imageFile.path), // <---- HERE I receive the ERROR!!
      );
    }
  }

我做错了什么?

有人有什么建议吗?

flutter flutter-layout
1个回答
0
投票

您可以在下面复制粘贴运行完整代码您需要FileImage

代码段

return CircleAvatar(
        radius: 80.0,
        backgroundImage: FileImage(imageFile),
      );

完整代码

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget { 
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(      
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  File imageFile;

  void _incrementCounter() async{
    var photo = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      imageFile = photo;
    });

    setState(() {     
      _counter++;
    });
  }

  Widget _ImageView() {
    if (imageFile == null) {
      return CircleAvatar(
        radius: 80.0,
        backgroundImage: AssetImage('assets/images/avatar_blank.jpeg'),
      );
    } else {
      return CircleAvatar(
        radius: 80.0,
        backgroundImage: FileImage(imageFile),
      );
    }
  }

  @override
  Widget build(BuildContext context) {   
    return Scaffold(
      appBar: AppBar(        
        title: Text(widget.title),
      ),
      body: Center(       
        child: Column(          
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            _ImageView(),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), 
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.