tf_album_saver 0.2.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 70

tf_album_saver #

A plugin that can save image/gif/video to an album.

Features #

Save image(jpg/jpeg/png)、gif、video to album

Usage #

To use this plugin, add tf_ablum_saver as a dependency in your pubspec.yaml file. For example:

    dependencies:
        tf_album_saver: ^0.2.1

Android and iOS specific permissions #

For this plugin to work you will have to add permission configuration to your AndroidManifest.xml (Android) and Info.plist (iOS) files. This will tell the platform which hardware or software features your app needs. Complete lists of these permission options can be found in our example app here:

AndroidManifest.xml #

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

Info.plist #

    Privacy - Photo Library Additions Usage Description
    Privacy - Photo Library Usage Description

0.2.1 #

  • ext.kotlin_version = '1.3.0'

0.2.0 #

  • Fix-iOS save gif
  • Add ImageType(jpg/jpeg/png) of Android

0.1.0 #

  • HotFix Changelog

0.0.2 #

  • Update FileType(image,gif,video,pdf)

0.0.1 #

  • Support image(jpg、jpeg、png)、gif、video

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf_viewer_plugin/pdf_viewer_plugin.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:tf_album_saver/tf_album_saver.dart';
import 'package:tf_toast/Toast.dart';
import 'package:video_player/video_player.dart';

import 'VideoPlayerHelper.dart';
import 'permission.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Title',
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  var type = FileType.image;
  var map = {
    FileType.image: 'images/jpg.jpg',
    FileType.gif: 'images/gif.gif',
    FileType.video: 'images/video.MOV',
    FileType.pdf: 'images/pdf.pdf',
  };

  var imgType = ImageType.jpg;
  var imgMap = {
    ImageType.jpg: 'images/jpg.jpg',
    ImageType.jpeg: 'images/jpeg.jpeg',
    ImageType.png: 'images/png.png',
  };

  //
  String pdfPath;
  VideoPlayerController videoController;

  @override
  void initState() {
    super.initState();

    saveToFile();
  }

  @override
  Widget build(BuildContext context) {
    var width = MediaQuery.of(context).size.width;
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('tf_album_saver')),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Container(
              height: 80,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  for (var t in map.keys)
                    GestureDetector(
                      child: Container(
                        alignment: Alignment.center,
                        width: width / map.keys.length * 0.6,
                        height: 50,
                        color: type == t ? Colors.red : Colors.transparent,
                        child:
                            Text('${t.toString().replaceAll('FileType.', '')}'),
                      ),
                      onTap: () => setState(() => type = t),
                    )
                ],
              ),
            ),
            if (type == FileType.image)
              Container(
                height: 40,
                width: width,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    for (var t in imgMap.keys)
                      GestureDetector(
                        child: Container(
                          alignment: Alignment.center,
                          width: width / map.keys.length / 3.0,
                          color: imgType == t ? Colors.red : Colors.transparent,
                          child: Text(
                            '${t.toString().replaceAll('ImageType.', '')}',
                          ),
                        ),
                        onTap: () => setState(() => imgType = t),
                      )
                  ],
                ),
              ),
            Container(
              width: width * 0.8,
              height: width * 0.8,
              child: type == FileType.video
                  ? AssetPlayerLifeCycle(map[type], (context, controller) {
                      videoController = controller;
                      return AspectRatioVideo(controller);
                    })
                  : type == FileType.pdf && pdfPath != null
                      ? PdfViewer(filePath: pdfPath)
                      : Image.asset(
                          type == FileType.gif ? map[type] : imgMap[imgType],
                          width: width * 0.8,
                          height: width * 0.8,
                        ),
            ),
            FlatButton(
              color: Colors.blue,
              child: Text('Save File To Album by FilePath'),
              onPressed: () async {
                var permissionGroup = Platform.isAndroid
                    ? PermissionGroup.storage
                    : PermissionGroup.photos;
                var b = await checkAndRequest(permissionGroup);
                if (!b) {
                  Toast.show(context, title: '请开启相册访问权限,以存储照片或Gif到相册');
                  return;
                }

                print('type = ${type.index}');
                var filePath = type == FileType.image
                    ? await getFile(imgMap[imgType])
                    : await getFile(map[type]);
                var errorMsg =
                    await TfAlbumSaver.saveToAlbum(type, filePath.path);
                var ret = errorMsg == null ? 'success' : errorMsg;
                var showText = 'Save File To Album by FilePath ${ret}';
                print(showText);
                Toast.show(context, title: showText);
              },
            ),
            if (type == FileType.image)
              FlatButton(
                color: Colors.blue,
                child: Text('Save Image To Album by Bytes'),
                onPressed: () async {
                  var permissionGroup = Platform.isAndroid
                      ? PermissionGroup.storage
                      : PermissionGroup.photos;
                  var b = await checkAndRequest(permissionGroup);
                  if (!b) {
                    Toast.show(context, title: '请开启相册访问权限,以存储照片或Gif到相册');
                    return;
                  }

                  final byteData = await rootBundle.load(imgMap[imgType]);
                  final bytes = byteData.buffer.asUint8List();

                  var errorMsg =
                      await TfAlbumSaver.saveImageByBytes(bytes, type: imgType);
                  var ret = errorMsg == null ? 'success' : errorMsg;
                  var showText = 'Save Image To Album by Bytes ${ret}';
                  print(showText);
                  Toast.show(context, title: showText);
                },
              ),
          ],
        ),
        floatingActionButton: type == FileType.video && videoController != null
            ? FloatingActionButton(
                onPressed: () {
                  setState(() {
                    videoController.value.isPlaying
                        ? videoController.pause()
                        : videoController.play();
                  });
                },
                child: Icon(
                  videoController.value.isPlaying
                      ? Icons.pause
                      : Icons.play_arrow,
                ),
              )
            : null,
      ),
    );
  }

  saveToFile() async {
    map.values.forEach(saveByName);
    imgMap.values.forEach(saveByName);
  }

  void saveByName(name) async {
    var file = await getFile(name);
    await file.createSync(recursive: true);
    final byteData = await rootBundle.load(name);
    final bytes = byteData.buffer.asUint8List();
    await file.writeAsBytesSync(bytes);

    print('filePath = ${file.path}');

    if (name.contains('pdf')) setState(() => pdfPath = file.path);
  }

  Future<File> getFile(String name) async {
    var documentsDir = await getApplicationDocumentsDirectory();
    return File("${documentsDir.path}/$name");
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  tf_album_saver: ^0.2.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:tf_album_saver/tf_album_saver.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
43
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
91
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.9+1
  • Flutter: 1.17.3

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:tf_album_saver/tf_album_saver.dart]

Package does not support Flutter platform macos

Because of import path [package:tf_album_saver/tf_album_saver.dart]

Package does not support Flutter platform web

Because of import path [package:tf_album_saver/tf_album_saver.dart]

Package does not support Flutter platform windows

Because of import path [package:tf_album_saver/tf_album_saver.dart]

Package not compatible with SDK dart

because of import path [tf_album_saver]

Health suggestions

Format lib/generated/i18n.dart.

Run flutter format to format lib/generated/i18n.dart.

Format lib/tf_album_saver.dart.

Run flutter format to format lib/tf_album_saver.dart.

Maintenance suggestions

The package description is too short. (-9 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.0
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test