Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 686|回复: 0

WebView中input file的解决方法

[复制链接]
  • TA的每日心情
    奋斗
    3 天前
  • 签到天数: 789 天

    [LV.10]以坛为家III

    2049

    主题

    2107

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    722638
    发表于 2021-7-3 12:57:52 | 显示全部楼层 |阅读模式
    public class MyWb extends Activity {
        /** Called when the activity is first created. */
        WebView web;
        ProgressBar progressBar;
    
        private ValueCallback<Uri> mUploadMessage;
        private final static int FILECHOOSER_RESULTCODE = 1;
    
        @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;
            }
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            web = (WebView) findViewById(R.id.webview01);
            progressBar = (ProgressBar) findViewById(R.id.progressBar1);
    
            web = new WebView(this);
            web.getSettings().setJavaScriptEnabled(true);
            web.loadUrl("http://www.script-tutorials.com/demos/199/index.html");
            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/*");
                    MyWb.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("*/*");
                    MyWb.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/*");
                    MyWb.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MyWb.FILECHOOSER_RESULTCODE);
                }
    
            });
            setContentView(web);
    
        }
    
        public class myWebClient extends WebViewClient {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
            }
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                return true;
    
            }
    
            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
                progressBar.setVisibility(View.GONE);
            }
        }
    
        // flipscreen not loading again
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
        }
    
        // To handle "Back" key press event for WebView to go back to previous
        // screen.
        /*
         * @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if
         * ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) { web.goBack();
         * return true; } return super.onKeyDown(keyCode, event); }
         */
    }

     

    三星、华为等android里webview不支持input file的解决方法

    由于安全因素android webview屏蔽了文件上传控件,但是他并没有完全封掉。

    <form method="POST" enctype="multipart/form-data">
    File to upload: <input type="file" name="uploadfile">&nbsp;&nbsp;
    <input type="submit" value="Press to Upload..."> to upload the file!
    </form>

    1.activity定义

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

    2.扩展WebChromeClient

    WebChromeClient chromeClient = new WebChromeClientImpl();
    view.setWebChromeClient(chromeClient);

    3.实现WebChromeClientImpl类

    private class WebChromeClientImpl extends WebChromeClient{
      
     //扩展支持alert事件
     @Override
     public boolean onJsAlert(WebView view, String url, String message,JsResult result) {
        Builder builder = new Builder(view.getContext());
        builder.setTitle("商机通提示").setMessage(message).setPositiveButton("确定", null);
        builder.setCancelable(false);
        builder.setIcon(R.drawable.ic_launcher);
        AlertDialog dialog = builder.create();
        dialog.show();
        result.confirm();
        return true;
     }
     //扩展浏览器上传文件
     //3.0++版本
     public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg, String acceptType) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");
        activity.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
     }
     //3.0--版本
     public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");
        activity.startActivityForResult(Intent.createChooser(i, "file Browser"),FILECHOOSER_RESULTCODE);
       }
     }

    以下是所有的android版本的一个完整的解决方案

    public class MyWb extends Activity {
     /** Called when the activity is first created. */
     WebView web;
     ProgressBar progressBar;
     private ValueCallback mUploadMessage;
     private final static int FILECHOOSER_RESULTCODE=1;
     
     @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;
      }
     }
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
    
     web = (WebView) findViewById(R.id.webview01);
     progressBar = (ProgressBar) findViewById(R.id.progressBar1);
     
     web = new WebView(this);
     web.getSettings().setJavaScriptEnabled(true);
     web.loadUrl("http://www.script-tutorials.com/demos/199/index.html");
     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 uploadMsg) {
          mUploadMessage = uploadMsg;
          Intent i = new Intent(Intent.ACTION_GET_CONTENT);
          i.addCategory(Intent.CATEGORY_OPENABLE);
          i.setType("image/*");
          MyWb.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("*/*");
        MyWb.this.startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
     }
    
     //For Android 4.1
     public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture){
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        MyWb.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MyWb.FILECHOOSER_RESULTCODE );
     }
     });
    
     setContentView(web);
    }
    
    public class myWebClient extends WebViewClient {
     @Override
     public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
     }
    
     @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        view.loadUrl(url);
        return true;
     }
    
     @Override
     public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
       super.onPageFinished(view, url);
       progressBar.setVisibility(View.GONE);
     }
    }
    
    //flipscreen not loading again
    @Override
    public void onConfigurationChanged(Configuration newConfig){
     super.onConfigurationChanged(newConfig);
    }
    
    // To handle "Back" key press event for WebView to go back to previous screen.
    /*@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
     if ((keyCode == KeyEvent.KEYCODE_BACK) &amp;&amp; web.canGoBack()) {
     web.goBack();
     return true;
     }
     return super.onKeyDown(keyCode, event);
    }*/
    }

    此外,我想补充一点, “上传页面”像在这个例子中, < 4个版本不会工作,因为它有一个图像预览功能,如果你想使它工作使用一个简单的php上传无预览。

    对于一些手机品牌修改了android浏览器,比如:三星,我们可以查看他们官网找到解决办法的。samsung developers

     

    转载自郑州网建-前端开发 http://camnpr.com/
    本文链接:http://camnpr.com/archives/1093.html

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-9-9 01:03 , Processed in 0.140849 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表