使用Flutter Share共享本地拍摄的图像

问题描述 投票:0回答:1

我有一个简单的相机应用,我想通过Share与他人分享我拍摄的图像。但是,当我运行以下代码时,它只是共享图片的路径:

Share.share(imagePath);

有人可以告诉我该怎么做吗?我认为您需要以某种方式转换位。谢谢!

这里有更多代码可供参考:

class DisplayPictureScreen extends StatelessWidget {
  final String imagePath;

  Future<ByteData> getBytesFromFile() async {
  Uint8List bytes = File(imagePath).readAsBytesSync() as Uint8List;
  return ByteData.view(bytes.buffer);
}

  const DisplayPictureScreen({Key key, this.imagePath}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Display the Picture')),
      // The image is stored as a file on the device. Use the `Image.file`
      // constructor with the given path to display the image.
      body: Image.file(File(imagePath)),
      floatingActionButton: FloatingActionButton(
        onPressed: () {

            print("in print");
            Share.share(imagePath);


            /*
          getBytesFromFile().then((bytes) {
    Share.file('Share via:', imagePath,
        bytes.buffer.asUint8List(), 'image/png');
  });
        */}
      )
    );
  }
}
android ios flutter share
1个回答
0
投票

您可以使用软件包https://pub.dev/packages/esys_flutter_share在您的代码中,您已经有bytes.buffer.asUint8List(),因此您可以直接使用它

await Share.file('esys image', 'esys.jpg', bytes.buffer.asUint8List(), 'image/jpg', text: 'My optional text.');
© www.soinside.com 2019 - 2024. All rights reserved.