簡體   English   中英

如何從 flutter 應用程序共享圖像文件到其他應用程序

[英]How to share Image file from flutter app to other apps

我有一個應用程序,該應用程序向用戶顯示一張圖像。 該圖像我將其作為鏈接保存在 MySQL 數據庫中,並將文件夾中的圖像保存到服務器中。 現在我嘗試讓用戶可以從我的應用程序將該圖像分享到其他應用程序,如 WhatsApp 或 Facebook。

我使用 share_plus 3.0.5 包來實現:

share_plus 3.0.5

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

通過此代碼獲取圖像:

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

}}); }

我試着讓它像那樣

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

但我得到錯誤:

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)

現在我需要知道如何讓用戶可以將該圖像分享給其他應用程序。

任何人都可以幫助我嗎?

正如您從文檔中看到的那樣, shareFiles()函數需要一個指向設備上本地路徑的字符串列表。 您傳遞的 URI (imageMaine) 不是本地路徑,因此插件會拋出異常。

如果你想分享一個鏈接,那么你應該使用share()函數。 如果你想共享一個文件,你應該首先獲取你的罰款,然后使用 shareFiles 函數發送它:

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

您可以使用我的庫app_utils ,它允許您使用提供的參數啟動 Android 和 iOS。 您可以將圖像 URI 作為參數傳遞給其他應用程序。

例如。

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

在此方法中傳遞用戶名和網絡圖像 URL,它將返回 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);
      }

像這樣調用這個方法,

XFile fileForShare = await fileFromImageUrl(
                      fileURL, empName);

Share.shareXFiles(
                    [fileForShare],
                    text: widget.postData.title.toString(),
                 );
  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();
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM