文件名与扩展名混合

时间:2019-03-01 14:19:56

标签: flutter

我正在尝试将图像上传到我的服务器。这是我的工作。

我使用image_picker

image_picker: ^0.4.12+1

这就是我从画廊中获取图片的方式

  Future getImage() async {
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      _image = image;
    });
  }

然后从我的Flatbutton上的onPressed

_upload() async {
    print(_image);
}

我明白了

I/flutter (19150): File: '/data/user/0/com.xxxx.xxxx/cache/image_picker1202934200258215491jpg'

所以我的问题是,为什么我的图像扩展名与文件名混合在一起,我该如何解决?

这是我的图像处理脚本

class _MainPageState extends State<MainPage> {
  bool _isLoggedIn = false;
  bool _isInAsyncCall = false;
  File _image;
  String _Fullname,_Email,_ProfilPict;

  TabController controller;


  Widget _Profil() {
    return SingleChildScrollView(
      child: new Center(
        child: Container(
            color: Colors.white30,
            child: new Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                _image == null ?
                 Container(
                    width: 150.0,
                    height: 150.0,
                    margin: EdgeInsets.only(top: 25.0, bottom: 10.0),
                     child: CircleAvatar(
                       radius: 30.0,
                       backgroundImage:
                       NetworkImage('https://via.placeholder.com/150'),
                       backgroundColor: Colors.transparent,
                     )
                ):
                Container(
                  height: 150.0,
                  width: 150.0,
                    margin: EdgeInsets.only(top: 25.0, bottom: 10.0),
                    child: CircleAvatar(
                      radius: 30.0,
                      backgroundImage: FileImage(_image),
                      backgroundColor: Colors.transparent,
                      child: Align(
                          alignment: Alignment.topRight,
                          child: IconButton(
                            icon: Icon(Icons.cancel),
                            onPressed: () {
                              setState(() {
                                _image = null;
                              });
                            },
                          )
                      ),
        )

                ),
                _image == null ?
                FlatButton(
                  child:Text("Edit"),
                  onPressed:getImage,
                  ):
                FlatButton(
                  child:Text("Upload"),
                  onPressed: _upload,
                ),

                  ],
            )),
      ),
    );
  }

   _upload() async {

    var stream = new http.ByteStream(DelegatingStream.typed(_image.openRead()));
    var length = await _image.length();
    var uri = Uri.parse("http://192.168.1.12:84/project/api/Upload_image");
    var request = http.MultipartRequest("POST", uri);
    var multipartFile = new http.MultipartFile('userfile', stream, length,
        filename: Path.basename(_image.path)
    );
    request.files.add(multipartFile);
    var response = await request.send();
    print(response.statusCode);
    response.stream.transform(utf8.decoder).listen((value) {
      print(value);
    });
  }

}

所以问题是当我使用android studio模拟器时,当我在真实设备上尝试时,它工作正常。

1 个答案:

答案 0 :(得分:1)

image获得的ImagePicker.pickImage的类型为File,如果需要打印路径,请按以下步骤进行操作:

var image = await ImagePicker.pickImage(source: ImageSource.gallery);
print(image.path);

如果您需要上传图片,请寻找MultipartRequest