user2918057
user2918057

Reputation: 199

Android WebView button input file

Sorry my english.. I have a web project, and I'll use in app for android, but in a part of the application must use input type="file", but it does not work in Android Webview, when trying to click on "Choose a File" it simply is static and does not open opening, but with browser url it works perfectly. Already tried several ways to get around this but without success. Anybody knows how can I do fixes.. please let me know...

public class MainActivity extends Activity {

    private String URL = "http://localhost:8080/myproject/upload.html";
    private WebView webView;
    private ValueCallback<Uri> uploadMessage;
    private final static int FILECHOOSER_RESULTCODE = 1;
    private static String registrationId = "";

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

}

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


        webView = (WebView) findViewById(R.id.container);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webView.setWebChromeClient(new WebChromeClient()  
        {  

            Intent i = new Intent(Intent.ACTION_GET_CONTENT);  

            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {  

                Log.i("openFileChooser", " IN ");

                i.addCategory(Intent.CATEGORY_OPENABLE);  
                i.setType("image/*");  
                startActivityForResult(Intent.createChooser(i,"File Chooser"), 222);  
            }


            @SuppressWarnings("unused")
            public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
                //             Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                Log.i("openFileChooser", " IN ");
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("*/*");
                startActivityForResult(Intent.createChooser(i, "File Browser"), 222);
            }


            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
                // openFileChooser(uploadMsg);
                Log.i("openFileChooser", " IN ");
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                i.addCategory(Intent.CATEGORY_OPENABLE);  
                i.setType("image/*");  
                startActivityForResult( Intent.createChooser( i, "File Chooser" ), 2223 );

            }

        }); 

        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(URL);

}

Upvotes: 3

Views: 2108

Answers (2)

ViliusK
ViliusK

Reputation: 11555

Found a SOLUTION which works for me. Add one more proguard rule:

-keepclassmembers class * extends android.webkit.WebChromeClient {
     public void openFileChooser(...);
}

Upvotes: 2

Jithu
Jithu

Reputation: 1478

You need to set some permissions in the WebChromeClient to achieve the same.

webView.setWebChromeClient(new WebChromeClient()  
    {  

        //The undocumented magic method override  
           //Eclipse will swear at you if you try to put @Override here  
        // For Android 3.0+
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {  
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
            i.addCategory(Intent.CATEGORY_OPENABLE);  
            i.setType("image/*");  
            startActivityForResult(Intent.createChooser(i,"File Chooser"), 222);  
           }

        // For Android 3.0+
           public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
//           Intent i = new Intent(Intent.ACTION_GET_CONTENT);
           i.addCategory(Intent.CATEGORY_OPENABLE);
           i.setType("*/*");
           startActivityForResult(
           Intent.createChooser(i, "File Browser"),
           222);
           }

        //For Android 4.1
           public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
//               openFileChooser(uploadMsg);
               Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
               i.addCategory(Intent.CATEGORY_OPENABLE);  
               i.setType("image/*");  
               startActivityForResult( Intent.createChooser( i, "File Chooser" ), 2223 );

           }

    }); 

Upvotes: 0

Related Questions