Flutter 照相机拍照和相册选择 以及拍照上传到服务器

18 篇文章 0 订阅
 void _upLoadFile() async{
      images= await ImagePicker.pickImage(source: ImageSource.gallery,maxHeight: 100);
      setState(() {
        CImages=images;
      });
    }
  void _take() async{
    images= await ImagePicker.pickImage(source: ImageSource.camera,maxHeight: 100);
    setState(() {
      CImages=images;
      _upLoad();
    });
  }

  void _upLoad() async{

    

    FormData formData = FormData.fromMap({
      "file":await MultipartFile.fromFile(CImages.path, filename: "aa.jpg")
    });
    Response response=   await new Dio().post("http://192.168.42.244:9102/file/upload",data: formData);
    print("上传成功===========${response.data}");
  }


    Widget _buildsImage(){
      if(CImages==null){
          return Text("重新选择");
      }else{
        return Image.file(CImages);
      }
    }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body:Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,

        children: <Widget>[
          RaisedButton(
            child: Text("打开相册"),
            onPressed: (){
              _upLoadFile();
            },
          ),
          RaisedButton(
            child: Text("拍照"),
            onPressed: (){
              _take();
            },
          ),
          _buildsImage()
        ],

      )
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值