如何将图像实例转换为flutter中的文件实例?

发布于 2025-02-10 09:15:25 字数 1013 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2025-02-17 09:15:25

您可以获取通往临时目录的路径:

final tempDir = await getTemporaryDirectory();

这样做之后,您可以在该临时目录中创建一个文件:

File fileToBeUploaded = await File('${tempDir.path}/image.png').create();

这样您的文件具有路径,并且已创建了一个实例。现在,您可以将文件写为:

fileToBeUploaded.writeAsBytesSync(uint8List);

现在,您可以将 filetobeuploaded 用作实际上是图像的文件。

完成代码:

final uint8List = await VideoThumbnail.thumbnailData(video: videoFile.path,);
final tempDir = await getTemporaryDirectory();
File fileToBeUploaded = await File('${tempDir.path}/image.png').create();
fileToBeUploaded.writeAsBytesSync(uint8List);
asyncFileUpload(fileToBeUploaded);

You can fetch the path to the temporary directory:

final tempDir = await getTemporaryDirectory();

After doing so, you can create a File in that temporary directory:

File fileToBeUploaded = await File('${tempDir.path}/image.png').create();

This way your file has a path and it's instance has been created. Now, you can write the file as:

fileToBeUploaded.writeAsBytesSync(uint8List);

Now, you can use fileToBeUploaded as File that is actually an image.

Complete code:

final uint8List = await VideoThumbnail.thumbnailData(video: videoFile.path,);
final tempDir = await getTemporaryDirectory();
File fileToBeUploaded = await File('${tempDir.path}/image.png').create();
fileToBeUploaded.writeAsBytesSync(uint8List);
asyncFileUpload(fileToBeUploaded);
夜深人未静 2025-02-17 09:15:25

由于您已经有UINT8列表,您可以尝试

File fileTpSend = File.fromRawPath(Uint8List uint8List);

Since you already have the uint8 list you can try

File fileTpSend = File.fromRawPath(Uint8List uint8List);
乖不如嘢 2025-02-17 09:15:25

基于您的代码,您需要导入'dart:io'和用户fromRawPathfile> file> file class> class> class> class(请参阅下面的摘要),

import 'dart:io';

final uint8List = await VideoThumbnail.thumbnailData(video:videoFile.path);
final imageAsFile = File.fromRawPath(uint8List);
await asyncFileUpload(imageAsFile);

但是此方法不执行f flutter网络工作

Based on your code you need to import 'dart:io' and user fromRawPath function from File class (check snippet below)

import 'dart:io';

final uint8List = await VideoThumbnail.thumbnailData(video:videoFile.path);
final imageAsFile = File.fromRawPath(uint8List);
await asyncFileUpload(imageAsFile);

But this method doesn't work for Flutter WEB

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文