与 Flutter 共享本地图像

Share local image with Flutter

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

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

如何分享这张本地图片?

我为 read/write 添加了这两行到本地存储:

    <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

首先要保存图片吗?并将其用于共享?怎么可能分享这张本地图片?

思路是share Uint8List

此演示使用 camera_camera 包的示例。 https://github.com/gabuldev/camera_camera/tree/master/example
camera_camera 软件包 https://pub.dev/packages/camera_camera 是一个很棒的软件包,具有制作精良的功能并在内部使用相机插件

代码片段
点击拍照后,系统return一个文件(本例中为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 中显示
文件路径显示在底部

camera插件官方例子,我只改了下面
代码片段

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