Discussions

Ask a Question
Back to All

How to upload an image to create an avartar video

I'd like to upload an image to create an avatar video.

I am a free user, but after testing I will upgrade the membership.

The below is Flutter app codes to upload an image from the phone.

I followed https://docs.heygen.com/reference/upload-talking-photo

final ImagePicker _picker = ImagePicker();
final pickedFile = await _picker.pickImage(source: ImageSource.gallery);
if (pickedFile == null) return;
File? _imageFile = File(pickedFile.path);

try {
final url = Uri.parse("https://upload.heygen.com/v1/talking_photo");

List imageBytes = await _imageFile!.readAsBytes();

final request = http.MultipartRequest("POST", url)
..headers['x-api-key'] = apiKey
// ..headers['Content-Type'] = 'image/jpeg'
..files.add(
http.MultipartFile.fromBytes(
"file",
imageBytes,
filename: "upload.jpg",
contentType: MediaType('image', 'jpeg'),
),
);

final response = await request.send();
final responseData = await response.stream.bytesToString();
final jsonResponse = json.decode(responseData);

if (jsonResponse['code'] == 100) {
setState(() {
_talkingPhotoId = jsonResponse['data']['talking_photo_id'];
_talkingPhotoUrl = jsonResponse['data']['talking_photo_url'];
});
} else {
_showError("Image Upload Fail: ${jsonResponse['message']}");
}
} catch (e) {
_showError("Image Upload Error: $e");
}

However it always returns "{code: 40001, message: talking photo data must be provided}" error.

Could you help me what is wrong?