首页 > 解决方案 > Flutter:在 Whatsapp 和 Facebook 共享图像时无法在预览中显示图像

问题描述

该图像可以在 Facebook Messenger 聊天中显示,但该图像无法在 Whatsapp 以及 Messenger 和 whatsapp 的预览图像中显示。知道我在哪里做错了吗?

Main.dart - 这是我捕获图像并将其保存到临时目录的位置。

  RenderRepaintBoundary boundary = globalKey.currentContext.findRenderObject();
  var image = await boundary.toImage(pixelRatio: 3.0);
  ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
  Uint8List pngBytes = byteData.buffer.asUint8List();

  final tempDir = await getTemporaryDirectory();
  final file = await new File('${tempDir.path}/image.png').create();
  await file.writeAsBytes(pngBytes);

  final channel = const MethodChannel('channel:me.amanda.share/share');
  channel.invokeMethod('shareFile', 'image.png'); 

MainActivity.java

private void shareFile(String path) {

try{
     File imageFile = new File(this.getCacheDir(), path);
     Uri contentUri = FileProvider.getUriForFile(this, "me.amanda.share", 
     imageFile);
     Intent shareIntent = new Intent(Intent.ACTION_SEND);
     shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     shareIntent.setData(contentUri);
     shareIntent.setType("image/png");

     shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
     this.startActivity(Intent.createChooser(shareIntent, "Share image 
     using"));
     }
     catch (Exception ex){
        //android.widget.Toast.makeText(this, ex.getMessage(), 
        Toast.LENGTH_SHORT).show();
     }
  }

文件路径.xml

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
    <cache-path name="image" path="."/>
    </paths>

标签: androidimageandroid-intentdartflutter

解决方案


对不起,伙计们,这是一个非常愚蠢的错误。图像是二维码,二维码的背景是透明的。因此,我应该将 qr 图像包装到 Container() 小部件中,并将容器设置为白色,RepaintBoundary 可以使用 QR 码捕获整个容器。我没有注意到预览图像和信使是黑暗的。

这是一个例子。

    hasValue ? Container(
    height: 230.0,
    color: Colors.white,
    margin: EdgeInsets.all(10.0),
    child: Center(
      child: RepaintBoundary(
        key: globalKey,
        child: Container(
          color: Colors.white,
          child: QrImage(
            data: !isScan ? _dataBarcodeString : trimBarcodeValue,
            size: 200.0,
          ),
        )
      ),
    ),

推荐阅读