Flutter share image - open failed: ENOENT (No such file or directory), null, null)

Viewed 1842

https://pub.dev/packages/share

Dependency: 
   share: ^0.6.5+2

Local file directory

Future<String> get _localPath async {
    final directory = await getApplicationDocumentsDirectory();
    return directory.path;
}

Share function

Future<void> _onShare(BuildContext context) async {
final RenderBox box = context.findRenderObject();
final path = await _localPath;

await Share.shareFiles(
    ['$path/assets/images/${widget.imgUrl}.png'],
    text: text,
    sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
}

Button - on press

FlatButton(child: Text('Share'), onPressed: () => _onShare(context)),

I'm using this package to share image from application. I have no issue sharing text but when I add the function to share image, I keep getting errors for missing image. Am I doing it correctly?

1 Answers

Managed to figure out the problem. Seems like I need to save image before I can reference the image.

final ByteData bytes = await rootBundle
    .load('assets/images/${widget.imgUrl}.png');
final Uint8List list = bytes.buffer.asUint8List();
final directory = (await getExternalStorageDirectory()).path;
File imgFile = File('$directory/screenshot.png');
imgFile.writeAsBytesSync(list);

Share.shareFiles(['$directory/screenshot.png'],
    sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
Related