In my case I want to send video to my host through post request.
I finish of many steps:
1- I can get a video file from the gallery with File Data Type. (Using image_picker).
videoSelectorGallery() async {
VideoFile = await ImagePicker.pickVideo(
source: ImageSource.gallery,
);
_videoPlayerController = VideoPlayerController.file(VideoFile)..initialize().then((_) {
setState(() { });
});
print("You selected gallery video : " + VideoFile.path);
.
.
.
}
2- I change it to String64bytes.
.
.
.
// Convert from file to String64byte
videoBytes = await VideoFile.readAsBytesSync();
base64Video = base64Encode(videoBytes);
print(base64Video);
}
3- I send String64bytes with post request.
.
.
.
var body = json.encode(
{
"shopName":...,
"description":...,
"typeID":...,
"video": base64Video // <---- String64bytes
}
);
.
.
.
The problem : When I send this request, it gets very long time to finish and the reason because the video has large size.
I think the best way to send faster is to compress the video but I don't know how and I search before about compressing videos useing flutter, I found some ways but still not work for me.
Any one know beeter idea how to send faster or good way to compress ?
Related
Im grabbing a picture from the gallery like described in the docs. that works, I can pick the pictures in my ios gallery and I am getting a full stream:
string localFilePath = System.IO.Path.Combine(FileSystem.CacheDirectory, photo.FileName);
using Stream sourceStream = await photo.OpenReadAsync();
using FileStream localFileStream = File.OpenWrite(localFilePath);
await sourceStream.CopyToAsync(localFileStream);
MainThread.BeginInvokeOnMainThread(() =>
{
img_profilePic.Source = ImageSource.FromStream(() => localFileStream);
img_profilePic.IsVisible = true;
});
But when I try to set the image to the imagesource of my image from xaml, nothing changes in the UI.
If I change the image to a local image however, it updates the UI. So it must be something with the stream.
Anyone got any idea?
is it possible to save the processed image as a File?
Here is what I'm trying to do, our app have a KYC (Know your customer) and we implemented the
face detection to make the users do several poses. What I want is to save them as an image file and upload it on the database
Example Scenario:
App ask the user to smile > The user smiled > save the image.
Here is what I have right now:
Where the app checks if the user smiled
if (faces.isNotEmpty) {
if (inputImage.inputImageData?.size != null &&
inputImage.inputImageData?.imageRotation != null) {
if (faces[0].smilingProbability! > 0.85) {
await _getImg();
}
}
}
Then I call a Function to stop image stream then take a picture (this works but on some physical device it crashes) but if I dont stop the image stream then called the takePicture() right-away it just crashes all the time.
_getImg() async {
setState(() {
globalBusy = true;
});
await _controller.stopImageStream();
var img = await _controller.takePicture();
VerificationVarHandler.livelinesImgsPaths.add(img.path);
}
As you can see it's not the best way at least for me I think, so maybe I can use the
inputImage from the _processCameraImage() because it has a byte? then I can pass that bytes to a decoder and save it locally when I trigger a function?
Or maybe better yet there is more elegant way of achieving this?
You can check out this gfg article - https://www.geeksforgeeks.org/face-detection-in-flutter-using-firebase-ml-kit/
Learn flutter in easiest way - https://auth.geeksforgeeks.org/user/ms471841/articles
Hello to everyone i hope you all good.
Im trying to upload a image to a server im using this:
But it dosent work. So please help me, i would really appreciate it.
Future<int> uploadFile(imageFile) async {
try {
var request = http.MultipartRequest(
'POST',
Uri.parse('http://10.0.2.2:8000/objects/'),
);
Map<String, String> headers = {"Content-type": "multipart/form-data"};
request.files.add(
http.MultipartFile(
'file',
imageFile.readAsBytes().asStream(),
imageFile.lengthSync(),
contentType: MediaType(
'image',
'jpeg',
),
),
);
request.headers.addAll(headers);
request.fields.addAll({
"app_label": "files",
"app_model": "file",
});
print("request: " + request.toString());
var res = await request.send();
print("This is response:" + res.toString());
return res.statusCode;
} catch (err) {
print(err);
return 502;
}
}
Response from the server and vscode:
https://ibb.co/2SMTBxJ
You can check the project here:
https://github.com/bRUNS123/hydra
Also if someone know how to put the image without crop like the "finished" file.
I the app you can take an image from the gallery or camera, then you have a preview and you can send or save to the gallery. But i created an option to avoid the crop image, but when i try to pass the information to not crop, nothing happen.
Also i wanna know if i can put all my void functions in a separated file.
Hope you have a great day!!
It's been 3 days that I try to fix an issue with the download of audio files with my Flutter application. When I try to download audio files, the request keep the "pending" status and finish with no error.
I have research a lot and find something about the contentLength of the client who is always at 0 but it doesn't help.
Now I have tried to make a get request to a website with sample audio files and it doesn't work too. I have tested via Postman and it always work.
My function:
Future<void> _download(String url, String filepath) async {
final response = await this.get("https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3");// await this.get("$baseURL$url");
log("Try to get audio files: ${response.isOk}");
if (response.isOk) {
File file = File(filepath);
final raf = file.openSync(mode: FileMode.write);
response.bodyBytes.listen((value) {
raf.writeFromSync(value);
}, onDone: () {
log("closed $filepath");
raf.closeSync();
});
}
}
The response.isOk is always false.
I used GetConnect from GetX package who used httpClient.
Via Dart devtools I obtain this from the request:
https://prnt.sc/1q3w33z
https://prnt.sc/1q3x9ot
So I used another package: Dio and now it works.
I am using api in flutter that is returning media data so how to handle this.
ScreenShot of my code
Post Man screenshot
Thanks in advance for your precious time.
Save your response to temp file or application document directory and add Image.file widget to your project to show this downloaded file. to save file you can use path_provider package.
if(response.statusCode == 200) {
var bytes = await consolidateHttpClientResponseBytes(response);
filePath = '$dir/$fileName';
file = File(filePath);
await file.writeAsBytes(bytes);
}
else
filePath = 'Error code: '+response.statusCode.toString();
}
also check this stackoverflow question and answers.