Android webview input type file

后端 未结 3 984
走了就别回头了
走了就别回头了 2021-01-03 03:05

I\'m trying to build web project using android, from webview. I have a input field of type file to let user upload files to server,

相关标签:
3条回答
  • 2021-01-03 03:21

    Webview class alone does not support file upload on facebook. You need to use both web chrome client and webview client to handle file upload on your android application as shown below. View more details and a working demo

    package com.whatsonline.androidphotouploadonfacebook;
    
    import android.app.Activity;
    import android.content.ActivityNotFoundException;
    import android.content.Intent;
    import android.content.res.Configuration;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.ViewGroup;
    import android.webkit.ValueCallback;
    import android.webkit.WebChromeClient;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.LinearLayout;
    
    /**
     * Created by sada on 6/17/2016.
     */
    public class upload extends Activity {
    
        WebView web;
        private ValueCallback<Uri> mUploadMessage;
        private final static int FILECHOOSER_RESULTCODE=1;
        LinearLayout ln1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.upload);
    
            web = new WebView(this);
            web.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    
            ln1=(LinearLayout) findViewById(R.id.ln1);
    
            WebSettings settings=web.getSettings();
            settings.setJavaScriptEnabled(true);
    
    
            web.loadUrl("http://www.facebook.com");
            web.setWebViewClient(new myWebClient());
    
            web.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) {
    
                    mUploadMessage = uploadMsg;
                    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                    i.addCategory(Intent.CATEGORY_OPENABLE);
                    i.setType("image/*");
                    upload.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
    
                }
    
                // For Android 3.0+
                public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
                    mUploadMessage = uploadMsg;
                    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                    i.addCategory(Intent.CATEGORY_OPENABLE);
                    i.setType("*/*");
                    upload.this.startActivityForResult(
                            Intent.createChooser(i, "File Browser"),
                            FILECHOOSER_RESULTCODE);
                }
    
                //For Android 4.1
                public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                    mUploadMessage = uploadMsg;
                    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                    i.addCategory(Intent.CATEGORY_OPENABLE);
                    i.setType("image/*");
                    upload.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), upload.FILECHOOSER_RESULTCODE);
    
                }
    
              });
            ln1.addView(web);
        }
        public class myWebClient extends WebViewClient
        {
            @Override
    
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
    
            }
    
            @Override
    
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
    
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        }
    
        //flipscreen not loading again
        @Override
        public void onConfigurationChanged(Configuration newConfig){
            super.onConfigurationChanged(newConfig);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    
            if(requestCode==FILECHOOSER_RESULTCODE){
                if (null == mUploadMessage) return;
                Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
                mUploadMessage.onReceiveValue(result);
                mUploadMessage = null;
    
            }
       }
        }
    
    0 讨论(0)
  • 2021-01-03 03:27

    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

    0 讨论(0)
  • 2021-01-03 03:43

    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

    0 讨论(0)
提交回复
热议问题