Flutter的SharePlus插件拒绝共享图像,声称“源文件不存在”。

huangapple go评论101阅读模式
英文:

Flutter's SharePlus plugin refusing to share images claiming 'The source file doesn't exist'

问题

Flutter的SharePlus插件拒绝分享图像,并报错:

> PlatformException(分享失败,assets/slideshow/tom_riddle_harry_potter_tour.webp:源文件不存在,null,null)

源文件确实存在,它在pubspec.yaml中正确声明,并且在应用程序的其他地方使用相同路径的Image.asset(...)正确显示。

我尝试了很多方法,尝试给XFile提供MimeType,但没有成功。
我想也许它在Web上不起作用,所以我在Web应用程序中取消了图像分享。

嗯,在Web应用程序中分享正常工作了,使用了一个备用方法,但问题在Android上仍然存在。所以我尝试省略图像的路径,只使用文件名(带扩展名)。但还是不行。

我尝试以不同的方式指定图像路径字符串,理论上不应该有效,但也不行。

我尝试阅读XFile的代码,就我所看到的,使用图像路径实例化XFile是完全有效的。

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")
    }
  });

我是否误解了XFile?我应该怎么做?

英文:

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?

答案1

得分: 2

尝试这个,

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", 
  // 其余的代码

要创建临时目录,如果您还没有的话,可能需要使用path_provider包。

https://pub.dev/packages/path_provider

英文:

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

答案2

得分: 0

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])
]);
英文:

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])
]);

huangapple
  • 本文由 发表于 2023年1月9日 04:49:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定