Android Webview解决input事件

webview调用input上传图片,记录下来

private ValueCallback<Uri> mUploadMessage;
private ValueCallback<Uri[]> mUploadCallbackAboveL;


private final static int FILECHOOSER_RESULTCODE = 1;


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


    initUI();
    initAV();
    setWebView();
}


private void initUI() {
    top_title_tv = (TextView) findViewById(R.id.top_title_tv);
    top_left_iv = (ImageView) findViewById(R.id.top_left_iv);
    webView = (WebView) findViewById(R.id.web);
    myProgressBar = (ProgressBar) findViewById(R.id.myProgressBar);
}




private void initAV() {
    Intent i = getIntent();
    String title = i.getStringExtra("title");
    url = i.getStringExtra("url");
    top_title_tv.setText(title);
    top_left_iv.setBackgroundResource(R.drawable.back);
    top_left_iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}


private void setWebView() {
    WebSettings ws = webView.getSettings();
    ws.setBuiltInZoomControls(true);


    ws.setDatabaseEnabled(true);
    String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
    //设置数据库路径
    ws.setDatabasePath(dir);


    ws.setUseWideViewPort(true);
    ws.setLoadWithOverviewMode(true);
    ws.setAllowFileAccess(true);//允许访问文件数据
    ws.setSaveFormData(true);
    ws.setJavaScriptEnabled(true);


    ws.setDefaultTextEncodingName("UTF-8");
    ws.setSupportZoom(true);
    ws.supportMultipleWindows();


    //启用地理定位
    ws.setGeolocationEnabled(true);
    ws.setDomStorageEnabled(true);
    ws.setSupportMultipleWindows(true);
    //webView.setWebViewClient(new WebViewClient());
    webView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }


        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }


        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }
    });




    webView.setWebChromeClient(new WebChromeClient(){
        // For Android 3.0+
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            Log.e(TAG, "openFileChoose(ValueCallback<Uri> uploadMsg)");
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");
            Web.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
        }


        // For Android 3.0+
        public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
            Log.e(TAG, "openFileChoose( ValueCallback uploadMsg, String acceptType )");
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");
            Web.this.startActivityForResult(
                    Intent.createChooser(i, "File Browser"),
                    FILECHOOSER_RESULTCODE);
        }
        //For Android 4.1
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
            Log.e(TAG, "4.1 openFileChoose(ValueCallback<Uri> uploadMsg, String acceptType, String capture)");
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");
            Web.this.startActivityForResult( Intent.createChooser( i, "File Browser" ), Web.FILECHOOSER_RESULTCODE );
        }
        // For Android 5.0+


        public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
            Log.e(TAG, "5.0 onShowFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)");
            mUploadCallbackAboveL = filePathCallback;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");
            Web.this.startActivityForResult(
                    Intent.createChooser(i, "File Browser"),
                    FILECHOOSER_RESULTCODE);
            return true;
        }
    });
    // String url="http://v.youku.com/v_show/id_XMTUzMjUwOTc2MA==_ev_1.html";
    webView.loadUrl(url);
}


@Override
protected void onResume() {
    super.onResume();
    webView.onResume();
//有的5.0的机型只显示一次选择图片的对话框  解决办法是加上这句
    if(mUploadCallbackAboveL!=null){
        mUploadCallbackAboveL.onReceiveValue(null);
    }
}


@Override
protected void onPause() {
    super.onPause();
    webView.onPause();
}


@Override
protected void onDestroy() {
    super.onDestroy();
    webView = null;
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    /**
     * 这里监控的是物理返回或者设置了该接口的点击事件
     * 当按钮事件为返回时,且WebView可以返回,即触发返回事件
     */
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if( webView.canGoBack())
        webView.goBack(); // goBack()表示返回WebView的上一页面
        else
        Web.this.finish();
        return true; // 返回true拦截事件的传递
    }
    return false;
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==FILECHOOSER_RESULTCODE)
    {
        if (null == mUploadMessage && null == mUploadCallbackAboveL) return;
        Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
        if (mUploadCallbackAboveL != null) {
            onActivityResultAboveL(requestCode, resultCode, data);
        }
        else  if (mUploadMessage != null) {
            mUploadMessage.onReceiveValue(result);
            mUploadMessage = null;
        }
    }
}


@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) {
    if (requestCode != FILECHOOSER_RESULTCODE
            || mUploadCallbackAboveL == null) {
        return;
    }


    Uri[] results = null;
    if (resultCode == Activity.RESULT_OK) {
        if (data == null) {


        } else {
            String dataString = data.getDataString();
            ClipData clipData = data.getClipData();


            if (clipData != null) {
                results = new Uri[clipData.getItemCount()];
                for (int i = 0; i < clipData.getItemCount(); i++) {
                    ClipData.Item item = clipData.getItemAt(i);
                    results[i] = item.getUri();
                }
            }


            if (dataString != null)
                results = new Uri[]{Uri.parse(dataString)};
        }
    }
    mUploadCallbackAboveL.onReceiveValue(results);
    mUploadCallbackAboveL = null;
    return;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值