我怎样才能在图库中保存图片?

How can i save pics in gallery?

这是打开相机按钮的代码 所以我想在拍照时将其保存在图库中 我使用 camera_camera 包 https://pub.dev/packages/camera_camera

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

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