Home > database >  Flutter's SharePlus plugin refusing to share images claiming 'The source file doesn't
Flutter's SharePlus plugin refusing to share images claiming 'The source file doesn't

Time:01-11

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?

CodePudding user response:

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

  • Related