【问题标题】:Flutter image upload from camera unable to send data to server in android mobile从相机上传的Flutter图像无法将数据发送到android mobile中的服务器
【发布时间】:2023-01-08 06:49:54
【问题描述】:

从相机上传的Flutter图像无法将数据发送到android手机中的服务器,

使用 Flutter image_picker 通过相机或图库选项上传图像,我可以在 UI 中打开和构建图像。

当发送到服务器图库图像工作正常,但相机选项图像不发送,任何改变或解决方案,

提前致谢

import 'package:image_picker/image_picker.dart';
import 'package:camera/camera.dart';    

void main(){
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({ Key? key }) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  XFile? _image; 
  final ImagePicker _picker = ImagePicker();
  var imageData;
  var filename;
  Future<void> _openImagePicker() async {
    final XFile? pickedFile = await _picker.pickImage(source: ImageSource.gallery);
    imageData = await pickedFile!.readAsBytes();
    setState(() {
      _image = pickedFile;      
    });
    filename = pickedFile.name;
  }

  Future<void> _openImageCameraPicker() async {
    final XFile? photo = await _picker.pickImage(source: ImageSource.camera);
    imageData = await photo!.readAsBytes();
    setState(() {
      _image = photo;
    });
    filename = photo.name;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
      appBar: AppBar(title: const Text("image upload")),
      body: Column(
        children: [
          FloatingActionButton(
            onPressed: () {
              _openImagePicker();              
            },
            child: const Icon(
              Icons.photo,
              color: Colors.white,
              size: 28.0,
            ),
          ),          
          const SizedBox(height: 30.0),          
          FloatingActionButton(
            onPressed: () {
              _openImageCameraPicker();              
            },
            child: const Icon(
              Icons.camera,
              color: Colors.white,
              size: 28.0,
            ),
          ),
        SizedBox(
          width: 100,
          height: 100,
          child: _image != null ? (kIsWeb)
              ? Image.network(_image!.path,  fit: BoxFit.cover)
              : Image.file(File(_image!.path), fit: BoxFit.cover) : const Text('No Image'),
         ),
         ElevatedButton(
          onPressed: () {
            uploadImage();
          },
          child: const Text("Upload", style: TextStyle(color: Colors.white)),
          ),
         Text(imageData.toString()),
      ]),
    )
    );
  }
  Future uploadImage() async {
   Map<String,String> headers = {
      'Content-Type':'application/json',
      };
      Map data = {
      'file' : (filename!=null) ? base64Encode(imageData) : '',
      'filename': (filename!=null) ? filename : ''
    };
    var response = await http.post(Uri.parse('http://127.0.0.1/api/upload-image'),  body: jsonEncode(data), headers: headers);
    if(response.statusCode == 200) {
      //succss      
    } else {
      // error
    }     
}
}

【问题讨论】:

  • 案例相机中的错误是什么?
  • 在移动相机中拍摄的照片并在 UI 中显示正常,但是当我点击上传按钮时它没有发送到服务器,如何在移动设备中获取错误日志,注意我没有使用任何模拟器,因为我的系统空间。在上面的代码中`imageData = await pickedFile!.readAsBytes();`图库文件上传它在用户界面中回显时获取字节数据,但使用相机imageData = await photo!.readAsBytes();但没有看到任何字节字符串

标签: flutter file-upload image-upload imagepicker


【解决方案1】:

兄弟,如果你不从相机或画廊发送图像,那么你可能可以发送你拍摄的 ss 图像 你必须在图像选择器中给出尺寸(最大高度和最大宽度)

最终图像 = await _picker.pickImage(来源:ImageSource.camera,maxHeight:200,maxWidth:200);

【讨论】:

    猜你喜欢
    • 2020-03-04
    • 1970-01-01
    • 2017-09-09
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 2015-07-16
    相关资源
    最近更新 更多