I would like to let the user choose several images. After confirmation I want to concatenate them and then share it with other apps. Concatenating works fine, I have written the final Image as a ByteStream using the image/image.dart library (image: ^2.1.4). For sharing I am using the esys library (esys_flutter_share: ^1.0.2).
import 'package:image/image.dart' as pkg_img;
Future<String> _joinImages(List<Type> ikonList) async {
Future<List> imageList() async {
// some logic to get the data
}
pkg_img.Image mergedImage;
final documentDirectory = await getApplicationDocumentsDirectory();
final String filename = "test.png";
final String fullFilePath = join(documentDirectory.path, "${filename}");
FutureBuilder(
future: imageList(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
} else {
List<pkg_img.Image> myList = snapshot.data;
mergedImage = someLogicToConcatenateImages(myList);
final file = new File(fullFilePath);
file.writeAsBytesSync(pkg_img.encodePng(mergedImage));
return null;
}
});
print(fullFilePath);
return await join(fullFilePath);
}
_shareImage(String fileName) async {
Uint8List bytes = await _readFileByte(fileName);
await Share.file('esys image', 'esys.png', bytes, 'image/png',
text: 'Sent from My App.');
}
Future<Uint8List> _readFileByte(String filePath) async {
Uri myUri = Uri.parse(filePath);
File myFile = new File.fromUri(myUri);
Uint8List bytes;
await myFile.readAsBytes().then((value) {
bytes = Uint8List.fromList(value);
print('reading of bytes is completed');
});
return bytes;
}
This whole code is being called when clicking on a button like so:
RaisedButton(
onPressed: () {
_joinImages(_ikonList).then(_shareImage);
},
child: Text('Export!'),
),
The error I run into is the following:
I/flutter (13768): File: '/data/user/0/com.myapp/app_flutter/test.png'
E/flutter (13768): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.myapp/app_flutter/test.png' (OS Error: No such file or directory, errno = 2)
E/flutter (13768): #0 _File.open.<anonymous closure> (dart:io/file_impl.dart:366:9)
E/flutter (13768): #1 _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter (13768): #2 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (13768): #3 _FutureListener.handleValue (dart:async/future_impl.dart:139:18)
E/flutter (13768): #4 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45)
E/flutter (13768): #5 Future._propagateToListeners (dart:async/future_impl.dart:709:32)
E/flutter (13768): #6 Future._completeWithValue (dart:async/future_impl.dart:524:5)
E/flutter (13768): #7 Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:554:7)
E/flutter (13768): #8 _rootRun (dart:async/zone.dart:1126:13)
E/flutter (13768): #9 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (13768): #10 _CustomZone.runGuarded (dart:async/zone.dart:925:7)
E/flutter (13768): #11 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
E/flutter (13768): #12 _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
E/flutter (13768): #13 _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
E/flutter (13768):