【问题标题】:Flutter Share Image/File ( mimeType issue )Flutter 共享图像/文件(mimeType 问题)
【发布时间】:2020-06-01 14:26:34
【问题描述】:

所以我需要使用 Flutter 将图像共享给另一个应用程序,我使用 esys_flutter_share 1.0.2 插件

Future<void> _shareImage(int index) async {
    final filename = _imageList[index].split("/")[2];
    try {
      final ByteData bytes = await rootBundle.load(_imageList[index]);
      await Share.file(
          'esys image', 'esys.png', bytes.buffer.asUint8List(), 'image/png',
          text: '$filename');
    } catch (e) {
      print('error: $e');
    }
  }

这是来自示例页面的相同代码,将我的图像从我的应用程序共享到另一个应用程序 Android/Ios 无法识别作为图像并作为文本共享的 mimeType 的唯一问题(在 Ios 模拟器和即使在真正的 ios 手机上)

Screenshot of the problem Screenshot how i need it

试过 'image/png' 、 'image/jpg' ecc,总是像文本文件一样共享文件。

【问题讨论】:

    标签: flutter dart mime-types


    【解决方案1】:

    用于 iOS 和 Android 的 share_extend 插件,用于共享文本、图像、视频和文件。

    使用:

    https://pub.dev/packages/share_extend#-installing-tab-

    代替:

    https://pub.dev/packages/esys_flutter_share#-installing-tab-

    首先,在 pubspec.yaml 文件中添加 share_extend 作为依赖项。

    dependencies:
    share_extend: "^1.1.7"
    

    将以下密钥添加到位于 /ios/Runner/Info.plist 中的 info.plist 文件,用于将共享图像保存到照片库。

    <key>NSPhotoLibraryAddUsageDescription</key>
    <string>describe why your app needs access to write photo library</string>
    

    如果您的项目需要共享外部存储文件的读写权限,请将以下权限添加到您的AndroidManifest.xml,位于/android/app/src/main/AndroidManifest.xml

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

    导入

    import 'package:share_extend/share_extend.dart';
    

    分享文字

    ShareExtend.share("Enter Text Here", "text"); //don't change second parameter it's 
    //defining the type of file to share.
    

    分享图片

    ShareExtend.share("Enter Image path Here", "image"); //don't change second parameter 
    //it's defining the type of file to share.
    

    分享视频

    ShareExtend.share("Enter Video path Here", "video"); //don't change second parameter 
    //it's defining the type of file to share.
    

    分享文件

    ShareExtend.share("Enter File path Here", "file"); //don't change second parameter 
    //it's defining the type of file to share.
    

    您还可以使用此库共享多个图像。

    【讨论】:

      【解决方案2】:

      发现我的问题,如果我让可选的字符串文本为空,插件将文件共享为图像文件,例如:

      final ByteData bytes = await rootBundle.load('assets/images/p1.jpg');
            await Share.file(
                'Preset image', '$filename.jpg', bytes.buffer.asUint8List(), '',
                text: '');
      

      enter image description here

      相反,如果我在可选描述字符串“文本:'我的可选文本。'”中写入任何内容,当我共享时,我会像原始问题一样共享文件。

      【讨论】:

        猜你喜欢
        • 2021-06-23
        • 1970-01-01
        • 2019-10-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-12
        • 1970-01-01
        • 1970-01-01
        • 2017-01-20
        相关资源
        最近更新 更多