【问题标题】:How can i save pics in gallery?我怎样才能将照片保存在图库中?
【发布时间】:2020-12-15 13:20:28
【问题描述】:

这是打开相机按钮的代码 所以我想当我拍照的时候,把它保存在画廊里 我使用 camera_camera 包 https://pub.dev/packages/camera_camera

【问题讨论】:

    标签: flutter camera gallery


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    第一步:将<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>添加到AndroidManifest.xml
    第二步:使用包https://pub.dev/packages/gallery_saver
    第 3 步:在 onFile 中使用 GallerySaver.saveImageNavigator.pop(context,file)
    代码sn-p

    Camera(
        onFile: (file) async {
          print("saver start");
          bool success = await GallerySaver.saveImage(file.path);
          print("saver end $success");
          Navigator.pop(context,file);
        },
    

    工作演示

    完整代码

    import 'dart:developer';
    import 'dart:io';
    
    import 'package:flutter/material.dart';
    import 'package:camera_camera/camera_camera.dart';
    import 'package:gallery_saver/gallery_saver.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("")),
            floatingActionButton: FloatingActionButton(
                child: Icon(Icons.camera_alt),
                onPressed: () async {
                  val = await showDialog(
                      context: context,
                      builder: (context) => Camera(
                            onFile: (file) async {
                              print("saver start");
                              bool success = await GallerySaver.saveImage(file.path);
                              print("saver end $success");
                              Navigator.pop(context,file);
                            },
                            mode: CameraMode.fullscreen,
                            //initialCamera: CameraSide.front,
                            //enableCameraChange: false,
                            //  orientationEnablePhoto: CameraOrientation.landscape,
                            onChangeCamera: (direction, _) {
                              print('--------------');
                              print('$direction');
                              print('--------------');
                            },
    
                            // imageMask: CameraFocus.square(
                            //   color: Colors.black.withOpacity(0.5),
                            // ),
                          ));
                  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"))));
      }
    }
    

    【讨论】:

    • 为什么这行 Navigator.pop(context,file);
    • 在源代码中。这是返回主屏幕的必要条件。
    猜你喜欢
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2013-05-31
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多