'how to convert list of base64encoded image to list of file
i have saved list of images to database in blob format and retriving it as well everything works well but i want to get all the images of database and convert it to list of file. in database i am saving list of images but seperating the images with comma like this :- image1,image2 but all image are in blob format now i want to retrive that image and convert that images to file and store it to my own list but so far i am only able to convert the first image of list and not other
List<File> imagesData = [];
Future<String> getImages()async {
imagesData.clear();
if (savedHomeworkImages != null) {
for (int i = 0; i < savedHomeworkImages.split(",").length; i++) {
if (savedHomeworkImages.split(",")[i].length > imagesData.length) {
Uint8List bytes = base64.decode(savedHomeworkImages.split(",")[1]);
String dir = (await getApplicationDocumentsDirectory()).path;
File file = File("$dir/" + DateTime.now().millisecondsSinceEpoch.toString() + ".jpg");
file.writeAsBytesSync(bytes);
setState(() {
imagesData.add(File(file.path));
});
print("This is images Data $imagesData");
return file.path;
}
}
}
}
#this is how i am trying to convert blob image to file but i am not able to convert list of blob to list of file need some guidence thanks
Solution 1:[1]
convert our Image file to bytes with the help of dart:io library.
import 'dart:io' as Io;
final bytes = await Io.File(image).readAsBytes();
// or
final bytes = Io.File(image).readAsBytesSync();
use dart:convert library base64Encode() function to encode bytes to Base64. It is the shorthand for base64.encode().
String base64Encode(List<int> bytes) => base64.encode(bytes);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | UD.. |