【问题标题】:Share local image with Flutter与 Flutter 共享本地图像
【发布时间】:2020-01-12 10:26:11
【问题描述】:

我想分享从 CameraController 拍摄的图像。

我文件的位置例如 /data/user/0/com.user.test/cache/2019-09-10 16:32:52.281842.png

如何共享此本地图像?

我添加了这两行用于本地存储的读/写:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

我使用来自https://pub.dev/packages/esys_flutter_share 的共享组件,效果很好。

void _sharePicture() async {
    print('Share picture');
    print(this.imagePath);

    final ByteData bytes = await rootBundle.load(this.imagePath);
    await Share.file('esys image', 'esys.png', bytes.buffer.asUint8List(), 'image/png', text: 'My optional text.');
  }

this.imagePath 是文件的本地位置::/data/user/0/com.user.test/cache/2019-09-10 16:32:52.281842.png

您必须先保存图像吗?以及将其用于共享?如何共享此本地图像?

【问题讨论】:

    标签: flutter dart share


    【解决方案1】:

    想法是分享Uint8List

    此演示使用 camera_camera 包的示例。 https://github.com/gabuldev/camera_camera/tree/master/example
    camera_camera 包https://pub.dev/packages/camera_camera 是一个很棒的包,有很好的功能并在里面使用相机插件

    代码 sn-p
    点击拍照后,系统返回一个文件(本例中为val),读取字节并传输到Uint8List

      print("path ${val}");
      List<int> bytes = await val.readAsBytes();
      Uint8List ubytes = Uint8List.fromList(bytes);
      await Share.file('ESYS AMLOG', 'amlog.jpg', ubytes, 'image/jpg');
    

    完整代码

    import 'dart:io';
    
    
    import 'package:flutter/material.dart';
    import 'package:camera_camera/camera_camera.dart';
    import 'dart:typed_data';
    import 'package:esys_flutter_share/esys_flutter_share.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: HomeScreen(),
        );
      }
    }
    
    class HomeScreen extends StatefulWidget {
      @override
      _HomeScreenState createState() => _HomeScreenState();
    }
    
    class _HomeScreenState extends State<HomeScreen> {
      File val;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: Text("Rully")),
            floatingActionButton: FloatingActionButton(
                child: Icon(Icons.camera_alt),
                onPressed: () async {
                  val = await showDialog(
                      context: context,
                      builder: (context) => Camera(
                            mode: CameraMode.fullscreen,
                            orientationEnablePhoto: CameraOrientation.landscape,
                            /*
                            imageMask: CameraFocus.square(
                              color: Colors.black.withOpacity(0.5),
                            ),
                            */
                          ));
    
                  print("path ${val}");
                  List<int> bytes = await val.readAsBytes();
                  Uint8List ubytes = Uint8List.fromList(bytes);
                  await Share.file('ESYS AMLOG', 'amlog.jpg', ubytes, 'image/jpg');
    
                  setState(() {});
                }),
            body: Center(
                child: Container(
                    height: MediaQuery.of(context).size.height * 0.7,
                    width: MediaQuery.of(context).size.width * 0.8,
                    child: val != null
                        ? Image.file(
                            val,
                            fit: BoxFit.contain,
                          )
                        : Text("Tire a foto"))));
      }
    }
    

    演示屏幕
    在 camera_camera 示例中,拍照按钮将在横向 mdoe 中显示
    底部显示文件路径

    对于相机插件官方示例,我只更改以下
    代码 sn -p

    void onTakePictureButtonPressed() {
        takePicture().then((String filePath) async{
          if (mounted) {
            setState(() {
              imagePath = filePath;
              videoController?.dispose();
              videoController = null;
            });
            if (filePath != null) {
              showInSnackBar('Picture saved to $filePath');
              File val = File(filePath);
              List<int> bytes = await val.readAsBytes();
              Uint8List ubytes = Uint8List.fromList(bytes);
              await Share.file('ESYS AMLOG', 'amlog.jpg', ubytes, 'image/jpg');
            }
          }
        });
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-31
      相关资源
      最近更新 更多