如何将图像实例转换为flutter中的文件实例?
我正在使用 videothumbanil class来获取这样的视频的 uint8list 这样的图像:
final uint8List = await VideoThumbnail.thumbnailData(video: videoFile.path,);
这样做之后,我将 uint8list 转换为图像使用以下代码:
Image image = Image.memory(uint8List);
我想做的是将此 image 转换为 file 类实例,以便我可以将此映像上传到我的服务器。在服务器上上传的代码为:
void asyncFileUpload(File file) async {
//create multipart request for POST or PATCH method
var request = http.MultipartRequest("POST", Uri.parse("127.0.0.1/upload"));
//create multipart using filepath, string or bytes
var pic = await http.MultipartFile.fromPath("image", file.path);
//add multipart to request
request.files.add(pic);
var response = await request.send();
//Get the response from the server
var responseData = await response.stream.toBytes();
var responseString = String.fromCharCodes(responseData);
print(responseString);
}
I am using VideoThumbanil class to fetch an Uint8List image of a video like this:
final uint8List = await VideoThumbnail.thumbnailData(video: videoFile.path,);
After doing so, i am converting the Uint8LIST to an Image using the following code:
Image image = Image.memory(uint8List);
What I want to do is to convert this image to a File class instance so that I can upload this image to my server. Code for uploading on server is:
void asyncFileUpload(File file) async {
//create multipart request for POST or PATCH method
var request = http.MultipartRequest("POST", Uri.parse("127.0.0.1/upload"));
//create multipart using filepath, string or bytes
var pic = await http.MultipartFile.fromPath("image", file.path);
//add multipart to request
request.files.add(pic);
var response = await request.send();
//Get the response from the server
var responseData = await response.stream.toBytes();
var responseString = String.fromCharCodes(responseData);
print(responseString);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以获取通往临时目录的路径:
这样做之后,您可以在该临时目录中创建一个文件:
这样您的文件具有路径,并且已创建了一个实例。现在,您可以将文件写为:
现在,您可以将 filetobeuploaded 用作实际上是图像的文件。
完成代码:
You can fetch the path to the temporary directory:
After doing so, you can create a File in that temporary directory:
This way your file has a path and it's instance has been created. Now, you can write the file as:
Now, you can use fileToBeUploaded as File that is actually an image.
Complete code:
由于您已经有UINT8列表,您可以尝试
Since you already have the uint8 list you can try
基于您的代码,您需要导入
'dart:io'
和用户fromRawPath
从file> file> file
class> class> class> class(请参阅下面的摘要),但是此方法不执行f flutter网络工作
Based on your code you need to import
'dart:io'
and userfromRawPath
function fromFile
class (check snippet below)But this method doesn't work for Flutter WEB