Skip to content

Latest commit

 

History

History
40 lines (30 loc) · 1.37 KB

File metadata and controls

40 lines (30 loc) · 1.37 KB

File and images

A simple demo app showing how to save images and files from Flutter and access the data from native widgets.

Flutter (save)lib/main.dart:

await HomeWidget.saveImage(_imageKey, imageProvider);
await HomeWidget.saveWidgetData(_imageTypeKey, imageType.name);
await HomeWidget.saveFile(
  _fileJsonKey,
  Uint8List.fromList(utf8.encode(json)),
  extension: 'json',
);

Android (path → file)ImageWidgetHomeWidget.kt, FileWidgetHomeWidget.kt:

val imagePath = prefs.getString(IMAGE_KEY, null)
BitmapFactory.decodeFile(imagePath)  // after `File(path).isFile` check
val jsonPath = prefs.getString(FILE_JSON_KEY, null)
File(jsonPath).readText(Charsets.UTF_8)  // when path exists and is a file

iOS (path → file)ImageWidgetHomeWidget/Widget.swift, FileWidgetHomeWidget/Widget.swift:

UserDefaults(suiteName: appGroupId)?.string(forKey: imageKey)  // then UIImage(contentsOfFile:)
UserDefaults(suiteName: appGroupId)?.string(forKey: fileJsonKey)  // then Data(contentsOf: URL(fileURLWithPath:))