Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to upload a file. #555

Closed
5 tasks done
Sunsiha opened this issue Oct 13, 2020 · 14 comments
Closed
5 tasks done

Not able to upload a file. #555

Sunsiha opened this issue Oct 13, 2020 · 14 comments
Labels
bug Something isn't working

Comments

@Sunsiha
Copy link

Sunsiha commented Oct 13, 2020

Environment

Technology Version
Flutter version 1.17.5
Plugin version 4.0.0+4
Android version 10
iOS version NA
Xcode version NA

Device information:

Description

Am trying to upload a file in my webview. It's working fine in iOS. It's not working in Android device. When I click on Choose file button first time it will open gallery and I can select an Image but it won't display in my view. Again if I click on Choose file button it won't open gallery itself. And am not getting any error or crash. The following code snippet you can use for reference. I tried the following link duplicate) But it doesn't work. Let me know where am doing mistake.

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:permission_handler/permission_handler.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await  PermissionHandler().requestPermissions(<PermissionGroup>[
    PermissionGroup.storage,
    PermissionGroup.camera,
    PermissionGroup.photos
  ]);
  runApp(new FAQsWebView());
}

var isFeedBackTouched;

class FAQsWebView extends StatefulWidget {
  @override
  FAQsWebViewState createState() => FAQsWebViewState();
}

class FAQsWebViewState extends State<FAQsWebView> {
  final GlobalKey previewContainer = new GlobalKey();
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  InAppWebViewController webView;
  int _page = 1;
  String initialUrl = 'https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_file';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    isFeedBackTouched = true;
  }

// ADD THIS LINE
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: ThemeData(accentColor: Colors.black),
        home: RepaintBoundary(
            key: previewContainer,
            child: Scaffold(
              resizeToAvoidBottomPadding: false,
              floatingActionButtonLocation:
                  FloatingActionButtonLocation.endFloat,
              key: _scaffoldKey,
              appBar: AppBar(
                brightness: Platform.isIOS ? Brightness.light : Brightness.dark,
                leading: IconButton(
                    icon: backImageStyle,
                    onPressed: () {
                      Navigator.pop(context);
                    }),
                backgroundColor: MoonlyteColors.actionBarColor,
                centerTitle: false,
                title: Text(
                  "File Upload",
                  style: appBarTextStyleEd,
                ),
              ),
              body: IndexedStack(
                index: _page,
                children: <Widget>[
                  InAppWebView(
                    initialUrl: initialUrl,
                    initialHeaders: {},
                    initialOptions: InAppWebViewGroupOptions(
                      crossPlatform: InAppWebViewOptions(
                        debuggingEnabled: true,
                        javaScriptEnabled: true,
                        javaScriptCanOpenWindowsAutomatically: true,
                        useOnDownloadStart: true,
                      ),
                      ios: IOSInAppWebViewOptions(),
                      android: AndroidInAppWebViewOptions(
                          domStorageEnabled: true,
                          databaseEnabled: true,
                          clearSessionCache: true,
                          thirdPartyCookiesEnabled: true,
                          allowUniversalAccessFromFileURLs: true,
                          allowFileAccess: true,
                          allowContentAccess: true),
                    ),
                    onWebViewCreated: (InAppWebViewController controller) {
                      webView = controller;
                    },

                    onLoadStop:
                        (InAppWebViewController controller, String url) {
                      setState(() {
                        _page = 0;
                      });
                    },
                  ),
                  Container(
                    child: const Center(
                      child: CircularProgressIndicator(),
                    ),
                  ),
                ],
              ),
            )));
  }
}

Using the version of flutter_inappwebview: ^4.0.0+4.

Added the following code in AndroidManifest file,

 <provider
            android:name="com.pichillilorenzo.flutter_inappwebview.InAppWebViewFileProvider"
            android:authorities="com.abc.sample.flutter_inappwebview.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external-path"
        path="."/>
    <external-cache-path
        name="external-cache-path"
        path="."/>
    <external-files-path
        name="external-files-path"
        path="."/>
    <files-path
        name="files_path"
        path="."/>
    <cache-path
        name="cache-path"
        path="."/>
    <root-path
        name="name"
        path="."/>
</paths>

Expected behavior:
Able to choose a file from the gallery.
Current behavior:

When I click on Choose file button first time it will open gallery and I can select an Image but it won't display in my view. Again if I click on Choose file button it won't open gallery itself. And am not getting any error or crash.

Steps to reproduce

  1. Copy the snippet of code.
  2. Run the application and click on choose a file button. It will open a gallery, select a file.
  3. You won't get any file.
  4. Click again on choose file button. It won't open a gallery itself.
  1. This
  2. Than that
  3. Then
@Sunsiha Sunsiha added the bug Something isn't working label Oct 13, 2020
@msarkrish
Copy link

I tried your code, It's working fine for me.
In AndroidManifest file replace authorities line by this line inside provider tag.

android:authorities="${applicationId}.flutter_inappwebview.fileprovider"

@Sunsiha
Copy link
Author

Sunsiha commented Oct 15, 2020

I tried your code, It's working fine for me.
In AndroidManifest file replace authorities line by this line inside provider tag.

android:authorities="${applicationId}.flutter_inappwebview.fileprovider"

Hi @msarkrish I have tried like that also..it's not working for me...

@msarkrish
Copy link

@Sunsiha I created new flutter project and added your code, it works fine. I try to upload files many time, it works correctly everytime.
I created new Flutter Project and added flutter_inappwebview plugin in pubspec.yaml file

flutter_inappwebview: ^4.0.0+4

I modified App minimum SDK to 17
Added this Provider tag inside manifest file

             <provider
                   android:name="com.pichillilorenzo.flutter_inappwebview.InAppWebViewFileProvider"
                   android:authorities="${applicationId}.flutter_inappwebview.fileprovider"
                   android:exported="false"
                   android:grantUriPermissions="true">
                   <meta-data
                        android:name="android.support.FILE_PROVIDER_PATHS"
                        android:resource="@xml/provider_paths" />
              </provider>

See my screenshot it app shows selected file.

Screenshot_2020-10-15-15-58-24-061_com example flutter_abstraction_example

@Sunsiha
Copy link
Author

Sunsiha commented Oct 15, 2020

which version of flutter and device @msarkrish

@msarkrish
Copy link

@Sunsiha I'm using Flutter 1.22.1 and My Device is Redmi Note 4(Nougat)

@Sunsiha
Copy link
Author

Sunsiha commented Oct 21, 2020

@Sunsiha I'm using Flutter 1.22.1 and My Device is Redmi Note 4(Nougat)

@msarkrish I really don't understand then why it is not working for me..just now I upgraded my flutter version also.
Flutter 1.22.2 • channel stable • https://github.com/flutter/flutter.git Framework • revision 84f3d28555 (5 days ago) • 2020-10-15 16:26:19 -0700 Engine • revision b8752bbfff Tools • Dart 2.10.2

@Sunsiha
Copy link
Author

Sunsiha commented Nov 3, 2020

Hi,
Finally I got a solution.

In manifest file added the following,
<meta-data android:name="flutterEmbedding" android:value="2" />

And in MainActivity.kt changed into the following,

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
  override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine);
  }
}

@Sunsiha Sunsiha closed this as completed Nov 3, 2020
@yfming93
Copy link

@Sunsiha I created new flutter project and added your code, it works fine. I try to upload files many time, it works correctly everytime.
I created new Flutter Project and added flutter_inappwebview plugin in pubspec.yaml file

flutter_inappwebview: ^4.0.0+4

I modified App minimum SDK to 17
Added this Provider tag inside manifest file

             <provider
                   android:name="com.pichillilorenzo.flutter_inappwebview.InAppWebViewFileProvider"
                   android:authorities="${applicationId}.flutter_inappwebview.fileprovider"
                   android:exported="false"
                   android:grantUriPermissions="true">
                   <meta-data
                        android:name="android.support.FILE_PROVIDER_PATHS"
                        android:resource="@xml/provider_paths" />
              </provider>

See my screenshot it app shows selected file.

Screenshot_2020-10-15-15-58-24-061_com example flutter_abstraction_example

It`s work well for me

@techproarvind
Copy link

android:resource="@xml/provider_paths"
Please give me this file location i set the res folder inside create a file xml then set provider_paths it's wrong ?
@yfming93

@jaroslawdabrowski
Copy link

Important note here about Redmi Phones. I am using Redmi Note 11 to debug my app. To do wireless debugging I had to disable MIUI optimalizations. This is causing some issues, e.g. the file picker opens but when you choose a file, nothing happens. I realised that this is not an issue of my app after I have tried to choose file in chrome and same issue happened. After enabling MIUI optimiazation file selection works fine.

@xfy6238
Copy link

xfy6238 commented Aug 4, 2023

Important note here about Redmi Phones. I am using Redmi Note 11 to debug my app. To do wireless debugging I had to disable MIUI optimalizations. This is causing some issues, e.g. the file picker opens but when you choose a file, nothing happens. I realised that this is not an issue of my app after I have tried to choose file in chrome and same issue happened. After enabling MIUI optimiazation file selection works fine.

Just like you; Redmi Note Android 10; After enabling MIUI optimiazation file selection works fine. But note only this package, I use this flutter_webview_pro ,This is also the situation. so I think this is not the problem of plug -in;

@anandStratBeans
Copy link

InAppWebview after select file not it's not transfer on server.

@FatihBaycu
Copy link

FatihBaycu commented Aug 23, 2023

https://stackoverflow.com/a/71968030/16504129
This worked for me. I used the code as is, I did not make any changes.

Copy link

github-actions bot commented Oct 7, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants