Gururaju Hiddenx
Gururaju Hiddenx

Reputation: 199

Android webview input type file

I'm trying to build web project using android, from webview. I have a input field of type file <input type="file" > to let user upload files to server, but it seem not to work on android webview, when I tap on the browse button, nothing happens.

Comp.java

package com.gururaju.bbmp;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;

public class Comp extends Activity {
    WebView comp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comp);

        WebView myWebView = (WebView) findViewById(R.id.comp);
        myWebView.setWebChromeClient(new WebChromeClient());
        myWebView.loadUrl("file:///android_asset/comp.html");

    }
}

activity_comp.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/comp"
        >

        </WebView>
</LinearLayout>

comp.html (in assets folder)

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="comp.css">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h2 align="center">Post your Complaints here</h2>
    <form enctype="multipart/form-data" action="" name="complaints" method="POST">
        <input class="title" type="text" name="title" placeholder="Enter the Complaint Title" /><br />

        <div class="spacer-welcome"></div>
        <textarea name="desc" class="desc" placeholder="Your complaint description here..."></textarea><br />
        <div class="spacer-welcome1"></div>

            <input id="center" type="file" name="image" ><br />
        <input class="upload" type="submit" name="submit" value="Submit" >
    </form>
</body>
</html>

Any help would be appreciated.

Upvotes: 5

Views: 14904

Answers (2)

Riad
Riad

Reputation: 3850

Try implementing the file Chooser method like this, as mentioned in the article link provided at the end:

webView.setWebChromeClient(new WebChromeClient() {

   // openFileChooser for Android 3.0+

    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){ 

     // Update message
     mUploadMessage = uploadMsg;

     try{  
         // do work....

     }catch(Exception e){

          Toast.makeText(getBaseContext(), "Exception:"+e,
          Toast.LENGTH_LONG).show();
     }
}

View Details Here

Upvotes: 0

caw
caw

Reputation: 31487

The answer by Riad points into the right direction, but that single callback is not enough to implement.

There are, in total, four hidden API methods that you have to implement. Their usage depends on the Android version. These methods are:

public void openFileChooser(ValueCallback<Uri> uploadMsg)
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)

You can use the following library which does all these things for you:

https://github.com/delight-im/Android-AdvancedWebView

Alternatively, you may take a look at the source code to see how it's done:

https://github.com/delight-im/Android-AdvancedWebView/blob/master/Source/src/im/delight/android/webview/AdvancedWebView.java

Upvotes: 2

Related Questions