plus_plugins icon indicating copy to clipboard operation
plus_plugins copied to clipboard

[Bug]: Failed to pass Uri in extra with key 'android.intent.extra.STREAM' when launch chooser

Open jifang opened this issue 2 years ago • 0 comments

Platform

Android 12

Plugin

android_intent_plus

Version

4.0.3

Flutter SDK

3.13.0

Steps to reproduce

I encountered an issue when attempting to launch a chooser to pass a video file as a Uri to the YouTube app using the provided code snippet. The goal was to utilize the AndroidIntent to send a video file with the appropriate data and flags.

final intent = AndroidIntent(
  action: 'android.intent.action.SEND',
  type: 'video/mp4',
  flags: [0x1], // FLAG_GRANT_READ_URI_PERMISSION
  data: videoPath,
  arguments: {
    'android.intent.extra.STREAM': videoPath,
    'android.intent.extra.TITLE': 'Hello World!',
  },
);
await intent.launchChooser('');

However, the console output displayed the following error:

12-14 20:54:58.775  8276  8276 W Bundle  : Key android.intent.extra.STREAM expected Parcelable but value was a java.lang.String.  The default value <null> was returned.
12-14 20:54:58.775  8276  8276 W Bundle  : Attempt to cast generated internal exception:
12-14 20:54:58.775  8276  8276 W Bundle  : java.lang.ClassCastException: java.lang.String cannot be cast to android.os.Parcelable
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.os.Bundle.getParcelable(Bundle.java:1007)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.content.Intent.getParcelableExtra(Intent.java:8917)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.content.Intent.migrateExtraStreamToClipData(Intent.java:12024)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.app.Instrumentation.execStartActivityAsCaller(Instrumentation.java:1973)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.app.Activity.startActivityAsCaller(Activity.java:5544)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at com.android.internal.app.chooser.DisplayResolveInfo.startAsCaller(DisplayResolveInfo.java:198)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at com.android.internal.app.ResolverActivity.safelyStartActivityInternal(ResolverActivity.java:1589)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at com.android.internal.app.ResolverActivity.safelyStartActivity(ResolverActivity.java:1546)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at com.android.internal.app.ResolverActivity$5.onAnimationEnd(ResolverActivity.java:2816)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.animation.Animator$AnimatorListener.onAnimationEnd(Animator.java:554)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1250)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1492)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.animation.AnimationHandler.access$100(AnimationHandler.java:37)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1008)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.view.Choreographer.doCallbacks(Choreographer.java:809)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.view.Choreographer.doFrame(Choreographer.java:740)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:995)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.os.Handler.handleCallback(Handler.java:938)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.os.Looper.loop(Looper.java:246)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.app.ActivityThread.main(ActivityThread.java:8633)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at java.lang.reflect.Method.invoke(Native Method)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)     

Upon investigating the issue, I have a local patch that may resolve this problem. The suggested patch checks the key, and if it corresponds to Intent.EXTRA_STREAM, it converts the String value to a Uri before putting it into the Bundle. I am seeking guidance on whether this is the correct fix, and if so, I am prepared to submit a pull request.

Patch:

private static Bundle convertArguments(Map<String, ?> arguments) {
    Bundle bundle = new Bundle();
    if (arguments == null) {
      return bundle;
    }
    for (String key : arguments.keySet()) {
      Object value = arguments.get(key);
      if (value instanceof Integer) {
        bundle.putInt(key, (Integer) value);
      } else if (value instanceof String) {
        Uri uri = null;
        if (key.equals(Intent.EXTRA_STREAM)) {
            uri = Uri.parse((String) value);
        }
        if (uri != null) {
            Log.d(TAG, "putParcelable URI:" + uri.toString());
            bundle.putParcelable(Intent.EXTRA_STREAM, uri);
        } else {            
            bundle.putString(key, (String) value);
        }
      } else if (value instanceof Boolean) {
      ...

Please advise on the recommended course of action. If further information is needed, I am happy to provide it.

Code Sample

No response

Logs

12-14 20:54:58.775  8276  8276 W Bundle  : Key android.intent.extra.STREAM expected Parcelable but value was a java.lang.String.  The default value <null> was returned.
12-14 20:54:58.775  8276  8276 W Bundle  : Attempt to cast generated internal exception:
12-14 20:54:58.775  8276  8276 W Bundle  : java.lang.ClassCastException: java.lang.String cannot be cast to android.os.Parcelable
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.os.Bundle.getParcelable(Bundle.java:1007)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.content.Intent.getParcelableExtra(Intent.java:8917)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.content.Intent.migrateExtraStreamToClipData(Intent.java:12024)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.app.Instrumentation.execStartActivityAsCaller(Instrumentation.java:1973)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.app.Activity.startActivityAsCaller(Activity.java:5544)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at com.android.internal.app.chooser.DisplayResolveInfo.startAsCaller(DisplayResolveInfo.java:198)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at com.android.internal.app.ResolverActivity.safelyStartActivityInternal(ResolverActivity.java:1589)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at com.android.internal.app.ResolverActivity.safelyStartActivity(ResolverActivity.java:1546)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at com.android.internal.app.ResolverActivity$5.onAnimationEnd(ResolverActivity.java:2816)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.animation.Animator$AnimatorListener.onAnimationEnd(Animator.java:554)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1250)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1492)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.animation.AnimationHandler.access$100(AnimationHandler.java:37)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1008)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.view.Choreographer.doCallbacks(Choreographer.java:809)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.view.Choreographer.doFrame(Choreographer.java:740)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:995)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.os.Handler.handleCallback(Handler.java:938)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.os.Looper.loop(Looper.java:246)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at android.app.ActivityThread.main(ActivityThread.java:8633)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at java.lang.reflect.Method.invoke(Native Method)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
12-14 20:54:58.775  8276  8276 W Bundle  : 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

Flutter Doctor

[✓] Flutter (Channel stable, 3.13.0, on macOS 14.1.2 23B92 darwin-arm64, locale en-US)
    • Flutter version 3.13.0 on channel stable at /Users/ji/Library/Developer/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision efbf63d9c6 (4 months ago), 2023-08-15 21:05:06 -0500
    • Engine revision 1ac611c64e
    • Dart version 3.1.0
    • DevTools version 2.25.0
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/ji/Library/Android/sdk/
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/ji/Library/Android/sdk/
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C65
    • CocoaPods version 1.13.0

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

[✓] Android Studio (version 2022.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 17.0.6+0-17.0.6b829.9-10027231)

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

[✓] Connected device (3 available)
    • SM G973U1 (mobile) • RF8M32CVNEW • android-arm64  • Android 11 (API 30)
    • macOS (desktop)    • macos       • darwin-arm64   • macOS 14.1.2 23B92 darwin-arm64
    • Chrome (web)       • chrome      • web-javascript • Google Chrome 120.0.6099.109
    ! Error: Browsing on the local area network for JiPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
    ! Error: Browsing on the local area network for JI11. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Checklist before submitting a bug

  • [X] I searched issues in this repository and couldn't find such bug/problem
  • [X] I Google'd a solution and I couldn't find it
  • [X] I searched on StackOverflow for a solution and I couldn't find it
  • [X] I read the README.md file of the plugin
  • [X] I'm using the latest version of the plugin
  • [X] All dependencies are up to date with flutter pub upgrade
  • [X] I did a flutter clean
  • [X] I tried running the example project

jifang avatar Dec 14 '23 13:12 jifang