flutter_inappwebview icon indicating copy to clipboard operation
flutter_inappwebview copied to clipboard

App crashes when upload image from File explorer on Android

Open faaqi opened this issue 10 months ago • 2 comments

  • [x] I have read the Getting Started section
  • [x] I have already searched for the same problem

Environment

Technology Version
Flutter version 3.19.3
Plugin version 6.0.0
Android version All
iOS version -
macOS version -
Xcode version -
Google Chrome version -

Device information: Tested on Samsung Zfold 5, Redmi Note 8, Samsung S22

Description

When I'm using InAppWebview to upload image from gallery, the app crashes when I tap on image to upload.

Expected behavior:

App must not crash and the image must be uploaded to webview.

Current behavior:

App crashes when I select image from gallery

Steps to reproduce

Try opening a website which requires KYC , when I tap Upload button on website, the Android app shows 2 options,

  1. Camera
  2. File Explorer

When I tap camera and upload image, it works fine and image uploads.

But when I tap file explorer and tap on image to upload, the app crashes from BG and file explorer also closes.

Custom Widget for the webview

import 'dart:collection';

import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:send_crypto/global/global.dart';
import 'package:send_crypto/service/haptic_provider/haptics_provider_service.dart';

typedef OnWebViewCreatedCallback = void Function(
    InAppWebViewController controller);

class CustomWebView extends StatefulWidget {
  final String title;
  final String url;
  final OnWebViewCreatedCallback onWebViewCreated;
  final String? injectJavascriptCode;
  final bool loading;

  const CustomWebView({
    required this.title,
    required this.url,
    required this.onWebViewCreated,
    this.loading = false,
    this.injectJavascriptCode,
  });

  @override
  State<CustomWebView> createState() => _CustomWebViewState();
}

class _CustomWebViewState extends State<CustomWebView> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.title,
          style: TextStyle(
            fontWeight: FontWeight.w600,
            color: Styles.colors.primaryWhite,
            fontSize: 20,
          ),
        ),
        centerTitle: true,
        leading: Container(
          alignment: Alignment.center,
          margin: EdgeInsets.only(left: 4),
          child: InkWell(
            onTap: () {
              HapticsProvider().backHaptic();
              CustomNavigation.pop(context);
            },
            child: Text(
              "Done",
              style: Styles.text.copyWith(
                fontSize: 17,
                color: Styles.colors.purplePrimary,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ),
      ),
      resizeToAvoidBottomInset: true,
      body: SafeArea(
        child: Stack(
          children: [
            // Webview
            Positioned.fill(
              child: InAppWebView(
                initialUrlRequest: URLRequest(url: WebUri(widget.url)),
                onWebViewCreated: (InAppWebViewController controller) {
                  widget.onWebViewCreated(controller);
                },
                onPermissionRequest: (controller, request) async {
                  return PermissionResponse(
                      resources: request.resources,
                      action: PermissionResponseAction.GRANT);
                },
                initialUserScripts: widget.injectJavascriptCode != null
                    ? UnmodifiableListView<UserScript>([
                        UserScript(
                          source: widget.injectJavascriptCode ?? '',
                          injectionTime:
                              UserScriptInjectionTime.AT_DOCUMENT_END,
                        ),
                      ])
                    : null,
              ),
            ),

            // Loading
            if (widget.loading)
              Container(
                color: Colors.black,
                child: Center(
                  child: CustomCircularLoading(),
                ),
              ),
          ],
        ),
      ),
    );
  }
}

Images

Stacktrace/Logcat

D/AndroidRuntime(29683): Shutting down VM
E/AndroidRuntime(29683): FATAL EXCEPTION: main
E/AndroidRuntime(29683): Process: com.sendcrypto.android, PID: 29683
E/AndroidRuntime(29683): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:73454 flg=0x1 }} to activity {com.sendcrypto.android/com.sendcrypto.android.MainActivity}: com.google.android.gms.common.api.ApiException: 16: 
E/AndroidRuntime(29683):        at android.app.ActivityThread.deliverResults(ActivityThread.java:5146)
E/AndroidRuntime(29683):        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5187)
E/AndroidRuntime(29683):        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
E/AndroidRuntime(29683):        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
E/AndroidRuntime(29683):        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
E/AndroidRuntime(29683):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2135)
E/AndroidRuntime(29683):        at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(29683):        at android.os.Looper.loop(Looper.java:236)
E/AndroidRuntime(29683):        at android.app.ActivityThread.main(ActivityThread.java:8037)
E/AndroidRuntime(29683):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(29683):        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
E/AndroidRuntime(29683):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
E/AndroidRuntime(29683): Caused by: com.google.android.gms.common.api.ApiException: 16: 
E/AndroidRuntime(29683):        at com.google.android.gms.internal.auth-api.zbbg.getPhoneNumberFromIntent(com.google.android.gms:play-services-auth@@20.7.0:4)
E/AndroidRuntime(29683):        at ru.surfstudio.otp_autofill.OTPPlugin.onActivityResult(OTPPlugin.kt:145)
E/AndroidRuntime(29683):        at io.flutter.embedding.engine.FlutterEngineConnectionRegistry$FlutterEngineActivityPluginBinding.onActivityResult(FlutterEngineConnectionRegistry.java:774)
E/AndroidRuntime(29683):        at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.onActivityResult(FlutterEngineConnectionRegistry.java:422)
E/AndroidRuntime(29683):        at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onActivityResult(FlutterActivityAndFragmentDelegate.java:857)
E/AndroidRuntime(29683):        at io.flutter.embedding.android.FlutterFragment.onActivityResult(FlutterFragment.java:1251)
E/AndroidRuntime(29683):        at io.flutter.embedding.android.FlutterFragmentActivity.onActivityResult(FlutterFragmentActivity.java:653)
E/AndroidRuntime(29683):        at android.app.Activity.dispatchActivityResult(Activity.java:8531)
E/AndroidRuntime(29683):        at android.app.ActivityThread.deliverResults(ActivityThread.java:5139)
E/AndroidRuntime(29683):        ... 11 more
I/Process (29683): Sending signal. PID: 29683 SIG: 9

faaqi avatar Apr 17 '24 22:04 faaqi

👋 @faaqi

NOTE: This comment is auto-generated.

Are you sure you have already searched for the same problem?

Some people open new issues but they didn't search for something similar or for the same issue. Please, search for it using the GitHub issue search box or on the official inappwebview.dev website, or, also, using Google, StackOverflow, etc. before posting a new one. You may already find an answer to your problem!

If this is really a new issue, then thank you for raising it. I will investigate it and get back to you as soon as possible. Please, make sure you have given me as much context as possible! Also, if you didn't already, post a code example that can replicate this issue.

In the meantime, you can already search for some possible solutions online! Because this plugin uses native WebView, you can search online for the same issue adding android WebView [MY ERROR HERE] or ios WKWebView [MY ERROR HERE] keywords.

Following these steps can save you, me, and other people a lot of time, thanks!

github-actions[bot] avatar Apr 17 '24 22:04 github-actions[bot]

@pichillilorenzo pls check this issue

faaqi avatar May 02 '24 09:05 faaqi

Update:

I was using otp_autofill pluging in my app. Removing that package resolved this issue and upload media started working fine.

faaqi avatar May 08 '24 07:05 faaqi

Closing this issue as this package had no issue in this exception. otp_autofill was causing the crash.

faaqi avatar May 10 '24 05:05 faaqi