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

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

从相机上传 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
    }     
}
}
flutter file-upload image-upload imagepicker
2个回答
2
投票

兄弟,如果您不发送来自

camera
gallery
的图像,那么您可能可以发送您拍摄的 ss 图像 你必须在
size(max height and max width )
中给出
image picker

final  image =  await  _picker.pickImage(source: ImageSource.camera,maxHeight: 200, maxWidth: 200);

0
投票

当您没有为图像选择器中的 getImage() 方法提供 maxHeight 和 maxWidth 时,就会发生这种情况。就按下面的方法做吧

await ImagePicker().getImage(source: source, maxHeight: 2200, maxWidth: 2200)
© www.soinside.com 2019 - 2024. All rights reserved.