4Q-s-r-o / signature

Flutter plugin that creates a canvas for writing down a signature
MIT License
255 stars 85 forks source link

Save image in internal directory #42

Closed GlauberAlc closed 3 years ago

GlauberAlc commented 3 years ago

I'm trying to save a png signature image in the internal directory, but it saves nothing. How do I save the png signature file in the internal directory?

My code:

onPressed: () async { if (_controller.isNotEmpty) {

                  final Uint8List? data = await _controller.toPngBytes();
                  if (data != null) {
                    Directory? directory = await getExternalStorageDirectory();
                    String path = directory!.path;
                    print(path);
                    var pngImage = await image.toByteData(format: _controller.ImageByteFormat.png);
                    await Directory('$path/assinaturaUser').create(recursive: true);
                    File('$path/$directoryName/$_controller').writeAsBytesSync(data.buffer.asUint8List());
                    final SharedPreferences prefs = await SharedPreferences.getInstance();
                    String base64Image = base64Encode(data);
                    prefs.setString("image", base64Image);
                    print(base64Image);
                  }
                }
              },
prooshani commented 3 years ago

Hey,

I did it this way:

import 'package:http/src/multipart_file.dart' as http;
import 'package:path_provider/path_provider.dart';

Future<String> get _localPath async {
  final directory = await getApplicationDocumentsDirectory();

  return directory.path;
}

Future<File> get _localFile async {
  final path = await _localPath;
  return File('$path/${DateTime.now()}.png');
}

Future saveSignature({@required Uint8List signData}) async {
  ///provide local path to store the signature
  final signFile = await _localFile;
  await File(signFile.path).writeAsBytes(signData.toList());

where signData is the signatureControllers.toPngBytes. just call the saveSignature and you can receive the signature path.

GlauberAlc commented 3 years ago

Hey man! Thank you!God bless you!