Flutter:将图片从 android 模拟器保存到资产文件夹中,并将其 post 保存到 API
Flutter : Save picture in asset folder from android emulator and post it to API
大家好,我是 Flutter 的初学者,我在 android 模拟器图库中有一张照片,我正在尝试通过 API 更新我用户的个人资料图片。
我的 API 功能:
这就是我更新它的方式(顺便说一下 phone 字段正在更新而图像没有)
我收到状态码 200
Future<Provider> updateProviderInfo(Provider person) async {
person.idUser = await FlutterSession().get("id");
final response = await http.put(
Uri.parse(ApiConstants.BASE_URL + ApiConstants.UPDATE_PROVIDER),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'providerId': person.idUser.toString(),
'providerProfilePicture': person.providerProfilePicture,
'phone' :"92211130030"
}),
);
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
Fluttertoast.showToast(
msg: "Updated with success",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.blue,
textColor: Colors.white,
fontSize: 16.0);
return Provider.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to update User Type :Service Provider.');
}}
我如何加载图片:
这是我从模拟器的图库或相机获取图像的方式:
Future<File> _loadImage(ImageSource imageSource) async {
PickedFile file = await _imagePicker.getImage(source: imageSource,imageQuality: 50, // <- Reduce Image quality
maxHeight: 250, // <- reduce the image size
maxWidth: 250);
File mFile;
if (null != file) {
Directory directory = await getTemporaryDirectory();
//mFile = await _saveImageToDisk(file.path, directory);
Map map = Map();
map['path'] = file.path;
String myLastFilePath = file.path.substring(file.path.indexOf("picker"));
print ("my try : "+myLastFilePath);
provider.providerProfilePicture = myLastFilePath;
print("my path : "+provider.providerProfilePicture);
map['directory'] = directory;
mFile = await compute(_saveImageToDisk, map);
}
return mFile; }
this is how my view looks like
我收到 200 状态代码,phone 号码正在更新,但数据库中的图像字段为空
my Route API test in postman
我想我应该使用 multiPartFormData,但我不知道如何使用它,我不确定是否会收到任何指导,谢谢
下面是使用多部分请求的代码:
static Future<bool> uploadFile(File file) async {
// open a byteStream
var stream = http.ByteStream(file.openRead());
stream.cast();
// get file length
var length = await file.length();
Uri uri = Uri.parse(ApiConstants.BASE_URL + ApiConstants.UPDATE_PROVIDER);
// create multipart request
var request = http.MultipartRequest("POST", uri);
// multipart that takes file
var multipartFile = http.MultipartFile(
'file',
stream,
length,
filename: basename(file.path),
);
// add file to multipart
request.files.add(multipartFile);
// send
var response = await request.send();
// print(response.statusCode);
if (response.statusCode == 200) {
...
} else {
...
}
}
根据您的需要更新此代码,看看它是否有效。
您可能需要稍微修改 server-side 代码以使其正常工作。
大家好,我是 Flutter 的初学者,我在 android 模拟器图库中有一张照片,我正在尝试通过 API 更新我用户的个人资料图片。
我的 API 功能: 这就是我更新它的方式(顺便说一下 phone 字段正在更新而图像没有) 我收到状态码 200
Future<Provider> updateProviderInfo(Provider person) async {
person.idUser = await FlutterSession().get("id");
final response = await http.put(
Uri.parse(ApiConstants.BASE_URL + ApiConstants.UPDATE_PROVIDER),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'providerId': person.idUser.toString(),
'providerProfilePicture': person.providerProfilePicture,
'phone' :"92211130030"
}),
);
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
Fluttertoast.showToast(
msg: "Updated with success",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.blue,
textColor: Colors.white,
fontSize: 16.0);
return Provider.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to update User Type :Service Provider.');
}}
我如何加载图片:
这是我从模拟器的图库或相机获取图像的方式:
Future<File> _loadImage(ImageSource imageSource) async {
PickedFile file = await _imagePicker.getImage(source: imageSource,imageQuality: 50, // <- Reduce Image quality
maxHeight: 250, // <- reduce the image size
maxWidth: 250);
File mFile;
if (null != file) {
Directory directory = await getTemporaryDirectory();
//mFile = await _saveImageToDisk(file.path, directory);
Map map = Map();
map['path'] = file.path;
String myLastFilePath = file.path.substring(file.path.indexOf("picker"));
print ("my try : "+myLastFilePath);
provider.providerProfilePicture = myLastFilePath;
print("my path : "+provider.providerProfilePicture);
map['directory'] = directory;
mFile = await compute(_saveImageToDisk, map);
}
return mFile; }
this is how my view looks like
我收到 200 状态代码,phone 号码正在更新,但数据库中的图像字段为空
my Route API test in postman
我想我应该使用 multiPartFormData,但我不知道如何使用它,我不确定是否会收到任何指导,谢谢
下面是使用多部分请求的代码:
static Future<bool> uploadFile(File file) async {
// open a byteStream
var stream = http.ByteStream(file.openRead());
stream.cast();
// get file length
var length = await file.length();
Uri uri = Uri.parse(ApiConstants.BASE_URL + ApiConstants.UPDATE_PROVIDER);
// create multipart request
var request = http.MultipartRequest("POST", uri);
// multipart that takes file
var multipartFile = http.MultipartFile(
'file',
stream,
length,
filename: basename(file.path),
);
// add file to multipart
request.files.add(multipartFile);
// send
var response = await request.send();
// print(response.statusCode);
if (response.statusCode == 200) {
...
} else {
...
}
}
根据您的需要更新此代码,看看它是否有效。 您可能需要稍微修改 server-side 代码以使其正常工作。