saveImageToGallery method
Implementation
Future<void> saveImageToGallery(String value) async {
try {
String base64String = value.replaceAll(RegExp(r'\s'), ''); // Remove unwanted spaces/newlines
int paddingLength = 4 - (base64String.length % 4);
if (paddingLength != 4) {
base64String += '=' * paddingLength; // Add padding
}
// Decode the Base64 string to a Uint8List (byte array)
Uint8List uint8List = base64Decode(base64String);
// Get the temporary directory to save the file
final directory = await getTemporaryDirectory();
final timestamp = DateTime.now().millisecondsSinceEpoch;
final path = '${directory.path}/screenshot_$timestamp.png';
// Convert Uint8List to File
final imageFile = File(path)..writeAsBytesSync(uint8List);
// Save the file to the gallery
final result = await GallerySaver.saveImage(imageFile.path);
if (result != null) {
Fluttertoast.showToast(
msg: "Screenshot save to gallery successfully",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
} else {
print('Failed to save image');
}
} on Exception catch (e) {
print('Failed to save image');
}
}