sendImageToAppGroups method
Implementation
Future sendImageToAppGroups(Map<String, dynamic> data) async {
if (appGroupId == null) {
throw Exception('appGroupId is null. Please call init() first.');
}
for (String key in data.keys) {
final value = data[key];
if (value is LiveActivityImage) {
Directory? sharedDirectory =
await FlutterAppGroupDirectory.getAppGroupDirectory(
appGroupId!,
);
Directory appGroupPicture =
Directory('${sharedDirectory!.path}/$kPictureFolderName');
Directory tempDir = await getTemporaryDirectory();
// create directory if not exists
appGroupPicture.createSync();
late File file;
late String fileName;
if (value is LiveActivityImageFromAsset) {
fileName = (value.path.split('/').last);
} else if (value is LiveActivityImageFromUrl) {
fileName = (value.url.split('/').last);
} else if (value is LiveActivityImageFromMemory) {
fileName = value.imageName;
}
final bytes = await value.loadImage();
file = await File('${tempDir.path}/$fileName').create();
file.writeAsBytesSync(bytes);
if (value.resizeFactor != 1) {
final buffer = await ImmutableBuffer.fromUint8List(bytes);
final descriptor = await ImageDescriptor.encoded(buffer);
final imageWidth = descriptor.width;
assert(
imageWidth > 0,
'Please make sure you are using an image that is not corrupt or too small',
);
final targetWidth = (imageWidth * value.resizeFactor).round();
file = await compressImage(file, targetWidth);
}
final finalDestination = '${appGroupPicture.path}/$fileName';
file.copySync(finalDestination);
data[key] = finalDestination;
_assetsCopiedInAppGroups.add(finalDestination);
// remove file from temp directory
file.deleteSync();
}
}
}