Home > Software design >  How to convert an Image instance to File instance in Flutter?
How to convert an Image instance to File instance in Flutter?

Time:06-26

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);
}

CodePudding user response:

Since you already have the uint8 list you can try

File fileTpSend = File.fromRawPath(Uint8List uint8List);

CodePudding user response:

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

  • Related