简体   繁体   English

使用 Flutter Share 分享一张本地拍摄的图片

[英]Using Flutter Share to share a locally taken image

I have a simple camera app I made and I want to be able to share the image I take to other people with Share.我有一个简单的相机应用程序,我希望能够通过 Share 将我拍摄的图像分享给其他人。 However, when I run the following code, it just shares the path to the picture:但是,当我运行以下代码时,它只是共享图片的路径:

Share.share(imagePath);

Can someone please tell me how to do this?有人可以告诉我怎么做吗? I think you need to convert the bits somehow.我认为您需要以某种方式转换这些位。 Thanks!谢谢!

Here is some more of the code for reference:下面是一些更多的代码供参考:

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');
  });
        */}
      )
    );
  }
}

You can use package https://pub.dev/packages/esys_flutter_share您可以使用包https://pub.dev/packages/esys_flutter_share
In your code, you already have bytes.buffer.asUint8List() , so you can directly use it在你的代码中,你已经有了bytes.buffer.asUint8List() ,所以你可以直接使用它

await Share.file('esys image', 'esys.jpg', bytes.buffer.asUint8List(), 'image/jpg', text: 'My optional text.');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM