I'm facing a problem from the server I'm getting the image url data as a string. I need to save all these urls in the XFile format list. Tell me how can I convert the image url to XFile format?
I get
I'm facing a problem from the server I'm getting the image url data as a string. I need to save all these urls in the XFile format list. Tell me how can I convert the image url to XFile format?
I get
Anyone can try this code if you also need the mime type or actual file extension,
Need 2 packages path_provider and mime.
import 'package:http/http.dart' show get;
Future<XFile> getImageXFileByUrl(String url) async {
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
String fileName = "image${DateTime.now().millisecondsSinceEpoch}";
File fileWrite = new File("$tempPath/$fileName");
Uri uri = Uri.parse(url);
final response = await get(uri);
fileWrite.writeAsBytesSync(response.bodyBytes);
final mimeType = lookupMimeType("$tempPath/$fileName", headerBytes: [0xFF, 0xD8]);
final type = mimeType.split("/");
final file = XFile("$tempPath/$fileName", mimeType: mimeType);
return file;
}
headerBytes: [0xFF, 0xD8] was found on the mime readme file.