skip to Main Content

Flutter’s SharePlus plugin is refusing to share images, complaining

PlatformException(Share failed, assets/slideshow/tom_riddle_harry_potter_tour.webp: The source file doesn’t exist., null, null)

The source file does exist, it’s correctly declared in pubspec.yaml, and displayed in the app elsewhere using Image.asset(... using the same path.

I’ve tried numerous things, I tried giving XFile the MimeType, that didn’t work.
I thought ‘maybe it just won’t work on web’, so I eliminated image sharing in the web app.

Well that got sharing in the web app working, using a fallback, but the problem persisted on Android. So I tried omitting the image’s path; just using the file name (with extension). Nope.

I tried specifying the image path string in a different manner, shouldn’t work, didn’t.

I tried reading the code for XFile and so far as I can see instantiating an XFile using an image path is perfectly valid.

Share.shareXFiles([XFile("assets/slideshow/${presentLocation.pictureList[0]}", 
mimeType: "image/webp", 
name: presentLocation.name.split(".")[0])],
subject: presentLocation.name + presentLocation.emoji,
text: shareStr,
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size).then((shareResult) async =>
                      {
                        if (shareResult.status ==
                            ShareResultStatus.success)
                          {
                            await MyApp.analytics.logShare(
                                contentType: "URL shared",
                                itemId: presentLocation.name,
                                method: "with image")
                          }
                      });

Am I misunderstanding XFile? What should I do?

2

Answers


  1. Try this,

     var asset = "assets/slideshow/${presentLocation.pictureList[0]}";
    
     ByteData imagebyte = await rootBundle.load(asset);
     final temp = await getTemporaryDirectory();
     final path = '${temp.path}/temp_image_name.png';
     File(path).writeAsBytesSync(imagebyte.buffer.asUint8List());
    
     await Share.shareXFiles(path, 
      mimeType: "image/webp", 
      // rest of code
    

    To create temporary directory, path-provider package might be needed if you already don’t have it.

    https://pub.dev/packages/path_provider

    Login or Signup to reply.
  2. One may also attempt to directly utilize the XFile.fromData code, as it does not necessitate any extra reliance on path-provider.

    String asset = "assets/slideshow/${presentLocation.pictureList[0]}";
    ByteData imagebyte = await rootBundle.load(asset);
    Share.shareXFiles([
        XFile.fromData(
            imagebyte.buffer.asUint8List(), 
            mimeType: 'image/webp', 
            name: presentLocation.pictureList[0])
    ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search