flutter图片上传
时间: 2023-09-06 13:08:14 浏览: 178
Flutter 图片上传可以通过以下步骤实现:
1. 导入相关依赖:
```dart
dependencies:
dio: ^4.0.0
flutter_image_compress: ^0.9.0
image_picker: ^0.8.3+2
```
2. 添加权限:
在 `AndroidManifest.xml` 中添加以下权限:
```xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
```
在 `Info.plist` 中添加以下权限:
```xml
<key>NSCameraUsageDescription</key>
<string>使用相机拍照上传图片</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>使用相册选取图片上传</string>
```
3. 实现选择图片功能:
```dart
final ImagePicker _picker = ImagePicker();
XFile? _imageFile;
Future<void> _getImage(ImageSource source) async {
final pickedFile = await _picker.pickImage(source: source);
if (pickedFile != null) {
setState(() {
_imageFile = pickedFile;
});
}
}
```
4. 实现图片压缩:
```dart
import 'dart:io';
import 'package:flutter_image_compress/flutter_image_compress.dart';
Future<File> _compressImage(File file) async {
final result = await FlutterImageCompress.compressAndGetFile(
file.absolute.path,
file.absolute.path,
quality: 50,
);
return result!;
}
```
5. 实现上传图片功能:
```dart
import 'dart:io';
import 'package:dio/dio.dart';
void _uploadImage() async {
if (_imageFile == null) return;
final file = File(_imageFile!.path);
final compressedFile = await _compressImage(file);
final formData = FormData.fromMap({
'file': await MultipartFile.fromFile(compressedFile.path),
});
final response = await Dio().post(
'https://example.com/upload',
data: formData,
);
if (response.statusCode == 200) {
print('上传成功');
} else {
print('上传失败');
}
}
```
完整代码示例:
```dart
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:image_picker/image_picker.dart';
class ImageUploadPage extends StatefulWidget {
const ImageUploadPage({Key? key}) : super(key: key);
@override
_ImageUploadPageState createState() => _ImageUploadPageState();
}
class _ImageUploadPageState extends State<ImageUploadPage> {
final ImagePicker _picker = ImagePicker();
XFile? _imageFile;
Future<void> _getImage(ImageSource source) async {
final pickedFile = await _picker.pickImage(source: source);
if (pickedFile != null) {
setState(() {
_imageFile = pickedFile;
});
}
}
Future<File> _compressImage(File file) async {
final result = await FlutterImageCompress.compressAndGetFile(
file.absolute.path,
file.absolute.path,
quality: 50,
);
return result!;
}
void _uploadImage() async {
if (_imageFile == null) return;
final file = File(_imageFile!.path);
final compressedFile = await _compressImage(file);
final formData = FormData.fromMap({
'file': await MultipartFile.fromFile(compressedFile.path),
});
final response = await Dio().post(
'https://example.com/upload',
data: formData,
);
if (response.statusCode == 200) {
print('上传成功');
} else {
print('上传失败');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('上传图片'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_imageFile != null)
Image.file(File(_imageFile!.path))
else
const Text('请选择图片'),
ElevatedButton(
onPressed: () => _getImage(ImageSource.camera),
child: const Text('拍照'),
),
ElevatedButton(
onPressed: () => _getImage(ImageSource.gallery),
child: const Text('选择图片'),
),
ElevatedButton(
onPressed: _uploadImage,
child: const Text('上传图片'),
),
],
),
),
);
}
}
```
阅读全文