Search code examples
flutterdartmime-types

Flutter Share Image/File ( mimeType issue )


So i need to share an image to another application with Flutter, i use esys_flutter_share 1.0.2 plugin

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

This is the same code from the example page, share my image from my application to another application the only problem that Android/Ios doesn't recognize the mimeType that is an image and shares as a text (tried on Ios simulator and even on real ios phone)

Screenshot of the problem Screenshot how i need it

Tried 'image/png' , 'image/jpg' ecc, always share the file like a text file.


Solution

  • share_extend plugin for iOS and Android for sharing text, image, video and files.

    use:

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

    instead of:

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

    First, add share_extend as a dependency in your pubspec.yaml file.

    dependencies:
    share_extend: "^1.1.7"
    

    Add the following key to your info.plist file, located in /ios/Runner/Info.plist for saving shared images to photo library.

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

    If your project needs read and write permissions for sharing external storage file, please add the following permissions to your AndroidManifest.xml, located in /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

    import 'package:share_extend/share_extend.dart';
    

    share text

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

    share image

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

    share video

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

    share file

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

    You can also share multiple images using this library.