1

I'm trying to make it possible to upload files via a web view but it is not happening currently, I'm looking at this famous answer and here are my

MainActivity.java

package ...;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    private ValueCallback<Uri> mUploadMessage;
    private final static int FILECHOOSER_RESULTCODE = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView wv = (WebView) findViewById(R.id.webView);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setWebViewClient(new WebViewClient());
        wv.setWebChromeClient(new WebChromeClient() {
            //The undocumented magic method override
            //Eclipse will swear at you if you try to put @Override here
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                MainActivity.this.showAttachmentDialog(uploadMsg);
            }

            // For Android > 3.x
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                MainActivity.this.showAttachmentDialog(uploadMsg);
            }

            // For Android > 4.1
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                MainActivity.this.showAttachmentDialog(uploadMsg);
            }
        });

        wv.loadUrl("http://www.tizag.com/phpT/fileupload.php");

    }

    private void showAttachmentDialog(ValueCallback<Uri> uploadMsg) {
        this.mUploadMessage = uploadMsg;

        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");

        this.startActivityForResult(Intent.createChooser(i, "Choose type of attachment"), FILECHOOSER_RESULTCODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == this.mUploadMessage) {
                return;
            }
            Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
            this.mUploadMessage.onReceiveValue(result);
            this.mUploadMessage = null;
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="....MainActivity">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

and permissions

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="...">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


</manifest>

If you set these 3 files and run it, I have set the webview url to a page which contains a file input, you will see it is not working. I am using API version 19+

I'm going to cry soon.

Community
  • 1
  • 1
php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
  • I forgot when, but the Android team has removed the undocumented `openFileChooser` methods sometime after 4.x. The way we got it to work universally is to implement a JavaBridge to invoke the file-chooser, and upload the files through a service. The form then receives a UUID which the server can use to resolve the uploaded file(s). It's complex, but it works regardless of SDK or the device's WebView. – 323go Jun 06 '16 at 14:12
  • @323go well, is there any resource that can get me through the process? – php_nub_qq Jun 06 '16 at 14:13
  • All the bits are there, but you'll have to glue them together yourself. Oh, to inject the GUID back into the form, we used javascript in the `loadUrl` calls. Making forms live through Android's Activity pause/resume is another set of issues -- and navigating to a fileChooser might well reset your form data unless you persist it. Suffice it to say, this is non-trivial. Pulling the client-code together and removing proprietary bits would take me a while, and I just don't have the time for it. Further, I don't even know if I still have access to the server-side code. – 323go Jun 06 '16 at 14:23

1 Answers1

0

So I came across this GIT repo that I think will help - https://github.com/GoogleChrome/chromium-webview-samples

Look at the "input-file-example".

I had issues with the Build.Gradle and found the solution here- Gradle DSL method not found: 'runProguard'

Hope this helps!

  • While these links may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – thewaywewere Jun 20 '17 at 00:48