ShekarMudaliyar / social_share

Flutter package to share images/videos/text on social media
https://pub.dev/packages/social_share
MIT License
112 stars 191 forks source link

shareInstagramStory BackgroundResourceImage not showing up when sharing #118

Closed Adimarogonas closed 1 year ago

Adimarogonas commented 1 year ago

Issues started for me after switching to 2.3.1 from before the appId. (using Flutter version 3.3.7) Problem: When I set the backgroundResourceImage before sharing the background image is not set. I checked with my implementation of getImageFileFromAssets() and the background image did show up when it was set as a sticker. I looked through the code itself as well as the iOS implementation and it seems to be fine so Im wondering if I'm doing something wrong. Code Sample:

//grab sticker image from cache
 final cache = DefaultCacheManager(); // Gives a Singleton instance
          final file = await cache.getSingleFile(path);

//Grab background image from assets
File background =
    await getImageFileFromAssets('images/share_screen.png');

await SocialShare.shareInstagramStory(
  imagePath: file.path,
  appId: "926460895001471",
  backgroundTopColor: "#ffffff",
  backgroundBottomColor: "#000000",
  attributionURL: "https://hobbimates.com",
  backgroundResourcePath: background.path);

Result: Screenshot 2022-12-05 at 7 16 38 PM

Expected: Screenshot 2022-12-05 at 7 20 07 PM

Flutter Doctor Output:

[✓] Flutter (Channel stable, 3.3.7, on macOS 13.0 22A380 darwin-arm, locale en-US)
    • Flutter version 3.3.7 on channel stable at
      /Users/andrewdimarogonas/Desktop/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision e99c9c7cd9 (5 weeks ago), 2022-11-01 16:59:00 -0700
    • Engine revision 857bd6b74c
    • Dart version 2.18.4
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/andrewdimarogonas/Library/Android/sdk
    • Platform android-33, build-tools 32.1.0-rc1
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14A400
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] VS Code (version 1.73.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.52.0

[✓] Connected device (4 available)
    • iPhone 12 (mobile)     • 00008101-001E30E4262B001E            • ios            •
      iOS 16.0.2 20A380
    • iPhone 14 Pro (mobile) • FB8702C0-75D4-46A0-9F7B-F18F1004CB29 • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-16-0 (simulator)
    • macOS (desktop)        • macos                                • darwin-arm64   •
      macOS 13.0 22A380 darwin-arm
    • Chrome (web)           • chrome                               • web-javascript •
      Google Chrome 107.0.5304.121

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
Adimarogonas commented 1 year ago

I figured out the solution

Future<File> getImageFileFromAssets(String path) async {
    final byteData = await rootBundle.load('assets/$path');
    DateTime now = DateTime.now();
    final file = File(
        '${(await getTemporaryDirectory()).path}/${now.toIso8601String()}');
    await file.writeAsBytes(byteData.buffer
        .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

    return file;
  }

did not write the png with a .png extension which

 if (_backgroundResourcePath != null) {
      var extension = _backgroundResourcePath.split(".").last;
      print("EXTENSION: ${extension}, ${_backgroundResourcePath}");
      if (["png", "jpg", "jpeg"].contains(extension.toLowerCase())) {
        args["backgroundImage"] = _backgroundResourcePath;
        print("Image");
      } else {
        args["backgroundVideo"] = _backgroundResourcePath;
        print("video");
      }
    }

failed to mime it as an image. I would say this is user error on my part so I will close this issue. for other users that may be stuck in imageFileFromAssets use this and specify your extension(you can also set it to detect your extension):

Future<File> getImageFileFromAssets(String path) async {
    final byteData = await rootBundle.load('assets/$path');
    DateTime now = DateTime.now();
    final file = File(
        '${(await getTemporaryDirectory()).path}/${now.toIso8601String()}.png');
    await file.writeAsBytes(byteData.buffer
        .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

    return file;
  }

Thank you for adding video support and appID to this package.