Search code examples
fluttersharefile-sharing

Flutter How to share File image via share plug in


Hello and thank you in advance!

I am working on image editing. I can pick the image from the image picker and rewrite it with the help of drawstringdrawstring. I can save the image in my documentDirectory. I want to share my image from documentDirectory path via the Share plugin. However, I am getting an error.

The code and error details are as shown below.

Code:

  FlatButton(
  child: Text("Apply Watermark Over Image"),
  onPressed: () async {
    final image1 = ui.decodeImage(_originalImage.readAsBytesSync());

    ui.drawString(image1, ui.arial_24, 300, 400, 'Hello And thank you');


    final documentDirectory = await getApplicationDocumentsDirectory();

    final file = new File(p.join(documentDirectory.path, "merged_image.jpg"));
   
    file.writeAsBytesSync(ui.encodeJpg(image1));
      // I HAVE THE IMAGE IN - documentDirectory.path "HERE I CANT SHARE THAT IMAGE" 
    final ByteData bytes = await rootBundle.load(documentDirectory.path);
    await Share.file('esys image', 'esys.png', bytes.buffer.asUint8List(), 'image/png', 
    text: 'My optional text.');
  },
)

I am getting this error :

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Unable to load asset: /data/user/0/com.photogranth.watermark/app_flutter


Solution

  • For share file image you can use this share_files_and_screenshot_widgets

      FlatButton(
      onPressed: () async {
      final directory = await getApplicationDocumentsDirectory();
    
          File f = File('${directory.path}/mycsv.csv');
    
          String csv = const ListToCsvConverter().convert(data);
    
          await f.writeAsString(csv);
    
          var file_as_bytes = await f.readAsBytes();
    
          Uint8List list = file_as_bytes.buffer.asUint8List();
    
          ShareFilesAndScreenshotWidgets().shareFile(
              "Title", "Name.csv", list, "text/csv",
              text: "This is the caption!");
                  },
    )