Reputation: 1
I have to download the image in flutter from the respond data, the imgae respond data is on stream type format how can I convert this stream type data into an image
Future _saveGallery(String imagepath) async { var data={ "key": imagepath, }; Response response = await dio.post( "/common-service/file-manager/v1/download-file",data: (data), ); print(response.statusCode); final result = await ImageGallerySaver.saveImage( Uint8List.fromList(response.data), quality: 60, name: "hello"); //print(result); }
Respond Data
�PNG IHDR j � ��NR sRGB ��� gAMA ���a pHYs t t�fx N�IDATx^�� �%E}?��ET��1���%�%*��D1rԠƅC0n��]����5�H��b41.D�57%QFAa������գ����{3S���9��n�{������U�o��
��n�7 [�@ @#j !P �� �F� 4B� �5 �� h�@ @#j !P �� �F� 4B� �5 �� h�@ @#j !P �� �F� 4B� �5 �� h�@ @#j !P �� �F� 4B� �5 �� h�@ @#j !P �� �F� 4B� �5 �� h�@ @#j !P �� �F� 4B� �5 �� h�@ @#j !P Ј�6Ps�e���Y�;���~��_-, hӚ����[ܥ�^ڝy��/~�%����E��N;u/x��6t/y�K��k�.�������w��vZw��w;���R ��4���O�}����������.(3b��g��W�JY���>��~�{�Sf�<����~���s��^���wO~�����!P l-�ԌJ��}�{_��W���m����w�9�t/|��#�8���o��T� �zl�Ϩ���?^~"�n���/���~���7��������8�M}���=����#��>����ۮ ���6Ps�g��8������ׯ/��I���@���<��}�ݻ}�ݷ;�䓻���-8 lQ���s�������O��^8���IOzRY6NfҼ���^��Wu�zԣ�{���嶨�}�s�W��ՅO l~�"P�y^��nv��F7��’���5��]������o-,
Upvotes: 0
Views: 512
Reputation: 1
I have found out solution for this. this is the api function I have used to get the stream type response image from the backend.
Future<dynamic> _saveGallery(String? imagepath) async {
var data={
"key": imagepath,
};
Response<dynamic> response = await dio.post(
"/common-service/file-manager/v1/download-file",data: (data),
options: Options(responseType: ResponseType.stream),
);
print(response.data);
ResponseBody responseBody = response.data;
String savePath = '/storage/emulated/0/Download/$imagename';
await downloadFile(responseBody, savePath);
//print(result);
}
this the function I have used for download the response image.
downloadFile(ResponseBody responseBody, String savePath) async {
final completer = Completer<void>();
try {
final file = File(savePath);
final sink = file.openWrite();
final stream = responseBody.stream;
stream.listen((data) {
sink.add(data);
}, onDone: () async {
await sink.flush();
await sink.close();
completer.complete();
}, onError: (error) {
completer.completeError(error);
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('File Downloading...'),
duration: Duration(seconds: 7),
),
);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
duration: Duration(seconds: 5),
content: Text('File downloaded'),
action: SnackBarAction(
label: 'Open',
onPressed: () {
OpenFilex.open(file.path);
},
),
),
);
//Navigator.push(context, MaterialPageRoute(builder: (context)=>yfimageview(file)));
} catch (error) {
completer.completeError(error);
}
return completer.future;
}
Upvotes: 0