3

I have app that app display one image to the user. that image I was saved it in MySQL database as a link and image in folder into server. Now I try to make user can share that image to other apps like WhatsApp or Facebook from my app.

I use share_plus 3.0.5 packages to make that:

share_plus 3.0.5

  await Share.shareFiles([//////////////////here/////////////], text: 'Image Shared');

Get image by this code:

  Future MakeShare() async {
var response = await http.get(
    Uri.parse("https://*********/ImageMakeShare.php?ID=" + widget.IDS.toString()),
    headers: {"Accept": "application/json"});

setState(() {

  var convertDataToJson = json.decode(response.body);
  dataImage = convertDataToJson['result'];
  if (dataImage != null) {

    imageMaine = dataImage[0]['image'];

}}); }

I try to make it like that

  await Share.shareFiles([imageMaine ], text: 'Image Shared');

But I get error:

E/flutter (10763): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(https:/*******0ee2e.png (No such file or directory), null, null, null)

Now I need to know how can I make user can share that image to other apps.

Anyone can help me?

M Al
  • 357
  • 7
  • 15

4 Answers4

7

As you can see from the documentation shareFiles() function expects a list of String pointing to a local path, on the device. You are passing a URI (imageMaine), which is not a local path, so the plugin throws and exception.

If you wanna share a link, than you should use the share() function. If you wanna share a file you should first fetch your fine and then send it with the shareFiles function:

  final url = Uri.parse("myLink");
  final response = await http.get(url);
  await File('/yourPath/myItem.png').writeAsBytes(response.bodyBytes);

  await Share.shareFiles(['/yourPath/myItem.png'], text: 'Image Shared');
Gabriel Costache
  • 859
  • 1
  • 6
  • 17
1

You can use my library app_utils which allows you to launch Android and iOS with provided parameters. You can pass your image URI to other applications as an arguments.

eg.

await AppUtils.launchApp( androidPackage: "com.whatsapp", iosUrlScheme: "whatsapp://", params: {"imageUrl": "https://image.png"});

  • Hello brother / Thank you a lot, but I do not share the image from a link, I get it from the database and then I share it, it is not a link / Also, I do not specify the application for the user who will send the image to it. – M Al Feb 18 '22 at 15:44
  • First, try to save your image in the File directory then share it using the file path. – Vishesh Pandey Feb 18 '22 at 16:14
  • Please be sure to read [Stack Overflow's self-promotion policy](https://stackoverflow.com/help/promotion) when referencing your own content. – Jeremy Caney Feb 19 '22 at 00:28
1
  static Future<String> shareScreenShort(String title, {required GlobalKey previewContainer, int originalSize = 1600}) async {
    try {
      RenderRepaintBoundary boundary = previewContainer.currentContext!.findRenderObject() as RenderRepaintBoundary;
      double pixelRatio = originalSize / MediaQuery.of(previewContainer.currentContext!).size.width;
      ui.Image image = await boundary.toImage(pixelRatio: pixelRatio);
      ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
      Uint8List pngBytes = byteData!.buffer.asUint8List();

      String filePathAndName = Service.documentDirectoryPath + '/images/$title.png';
      File imgFile = File(filePathAndName);
      await imgFile.writeAsBytes(pngBytes);
      await Share.shareXFiles(
        [XFile(imgFile.path)],
        sharePositionOrigin: boundary.localToGlobal(Offset.zero) & boundary.size,
        text: title,
      );

      return "success";
    } catch (ex) {
      return ex.toString();
    }
  }
Baby Love
  • 46
  • 3
-1

Pass the username and network image URL in this method, it will return XFile.

fileFromImageUrl(String url, String userName) async {
        final response = await http.get(
          Uri.parse(url),
        );
    
        final documentDirectory = await getApplicationDocumentsDirectory();
    
        var randomNumber = Random();
    
        final file = File(
          join(
            documentDirectory.path,
            "${randomNumber.nextInt(100)}_$userName.png",
          ),
        );
    
        file.writeAsBytesSync(response.bodyBytes);
    
        return XFile(file.path);
      }

call this method like this,

XFile fileForShare = await fileFromImageUrl(
                      fileURL, empName);

Share.shareXFiles(
                    [fileForShare],
                    text: widget.postData.title.toString(),
                 );
Nehil Koshiya
  • 1
  • 2
  • 6
  • 23