ShekarMudaliyar / social_share

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

Unable to share text with shareOptions #13

Closed IgorAnohin closed 4 years ago

IgorAnohin commented 4 years ago

Hi!

When I tried to share a text message on my Android phone (Redmi 6, Android 8.1) I caught the following error:

E/MethodChannel#social_share( 8354): Failed to handle method call
E/MethodChannel#social_share( 8354): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND flg=0x1 clip={null T:1qEfOimGcVx2ezjL2Ea8} (has extras) }
E/MethodChannel#social_share( 8354):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1945)
E/MethodChannel#social_share( 8354):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1620)
E/MethodChannel#social_share( 8354):    at android.app.Activity.startActivityForResult(Activity.java:4541)
E/MethodChannel#social_share( 8354):    at android.app.Activity.startActivityForResult(Activity.java:4499)
E/MethodChannel#social_share( 8354):    at android.app.Activity.startActivity(Activity.java:4860)
E/MethodChannel#social_share( 8354):    at android.app.Activity.startActivity(Activity.java:4828)
E/MethodChannel#social_share( 8354):    at com.shekarmudaliyar.social_share.SocialSharePlugin.onMethodCall(SocialSharePlugin.kt:116)
E/MethodChannel#social_share( 8354):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:231)
E/MethodChannel#social_share( 8354):    at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:93)
E/MethodChannel#social_share( 8354):    at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:642)
E/MethodChannel#social_share( 8354):    at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#social_share( 8354):    at android.os.MessageQueue.next(MessageQueue.java:331)
E/MethodChannel#social_share( 8354):    at android.os.Looper.loop(Looper.java:149)
E/MethodChannel#social_share( 8354):    at android.app.ActivityThread.main(ActivityThread.java:6662)
E/MethodChannel#social_share( 8354):    at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#social_share( 8354):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
E/MethodChannel#social_share( 8354):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
E/flutter ( 8354): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, No Activity found to handle Intent { act=android.intent.action.SEND flg=0x1 clip={null T:1qEfOimGcVx2ezjL2Ea8} (has extras) }, null)
E/flutter ( 8354): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter ( 8354): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33)
E/flutter ( 8354): <asynchronous suspension>
E/flutter ( 8354): #2      SocialShare.shareOptions (package:social_share/social_share.dart:217:41)
E/flutter ( 8354): #3      _buildScaffold.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:circles_app/presentation/keys/keys_screen.dart:45:35)
E/flutter ( 8354): #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter ( 8354): #5      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
E/flutter ( 8354): #6      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter ( 8354): #7      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter ( 8354): #8      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter ( 8354): #9      BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:236:7)
E/flutter ( 8354): #10     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter ( 8354): #11     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
E/flutter ( 8354): #12     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter ( 8354): #13     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter ( 8354): #14     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter ( 8354): #15     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter ( 8354): #16     _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter ( 8354): #17     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter ( 8354): #18     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter ( 8354): #19     _invoke1 (dart:ui/hooks.dart:273:10)
E/flutter ( 8354): #20     _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
E/flutter ( 8354): 

I checked how sharing is realized in another library - share. They explicitly add text/plain type to Intent. link to the line of code

I think a possible solution is to manually assign the type if the image is null, like so: SocialSharePlugin.kt

          if(image!=null){
              //check if  image is also provided
              val imagefile =  File(registrar.activeContext().cacheDir,image)
              val imageFileUri = FileProvider.getUriForFile(registrar.activeContext(), registrar.activeContext().applicationContext.packageName + ".com.shekarmudaliyar.social_share", imagefile)
              intent.type = "image/*"
              intent.putExtra(Intent.EXTRA_STREAM,imageFileUri)
          } else {
              intent.type = "text/plain";
          }

Or we may assign "text/plain" type by default and change it if image != null.

@ShekarMudaliyar What do you think about this fix? Should I open PR?

ShekarMudaliyar commented 4 years ago

if you feel it would help then sure go ahead

NWalker1208 commented 4 years ago

This is causing me problems as well. Is the PR being worked on?

ShekarMudaliyar commented 4 years ago

Thanks for ur contribution @NWalker1208 it works great now