1. 程式人生 > >WebView 支援上傳圖片

WebView 支援上傳圖片

今天公司突然上傳了一個網頁有上傳圖片功能,但是WebView沒有支援,沒辦法只能再寫些程式碼。然後就ok了,效果如下圖所示:
這裡寫圖片描述
實現關鍵程式碼如下所示:


    @SuppressLint("NewApi")
    private void initmWebView() {
        mWebView.getSettings().setJavaScriptEnabled(true); // enable javascript
        //支援H5本地儲存 enable local storage
        mWebView.getSettings().setDatabaseEnabled(true
); String path = getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath(); mWebView.getSettings().setDatabasePath(path); mWebView.getSettings().setDomStorageEnabled(true); mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); mWebView.getSettings().setUseWideViewPort(true
); mWebView.getSettings().setLoadWithOverviewMode(true); mWebView.getSettings().setSupportZoom(true); mWebView.getSettings().setBuiltInZoomControls(true); if (android.os.Build.VERSION.SDK_INT >= 11) { mWebView.getSettings().setDisplayZoomControls(false
); } else { // 3.0一下可用反射解決 } mWebView.getSettings().setAppCacheEnabled(true); mWebView.requestFocus(); mWebView.setWebChromeClient(wcc); mWebView.setDownloadListener(dl); mWebView.setWebViewClient(wvc); mWebView.loadUrl(url); } 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; } } private WebChromeClient wcc = new WebChromeClient() { @SuppressWarnings("unused") public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { mUploadMessage = uploadMsg; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "完成操作需要使用"), FILECHOOSER_RESULTCODE); } @SuppressWarnings("unused") public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { mUploadMessage = uploadMsg; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "完成操作需要使用"), FILECHOOSER_RESULTCODE); } @SuppressWarnings("unused") public void openFileChooser(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "完成操作需要使用"), FILECHOOSER_RESULTCODE); } //網頁進度情況 public void onProgressChanged(WebView view, int newProgress) { if (newProgress < 100) { mBar.setVisibility(View.VISIBLE); mBar.setProgress(newProgress); } else { mBar.setVisibility(View.GONE); } }; public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) { AlertDialog.Builder b2 = new AlertDialog.Builder(MainActivity.this).setTitle("溫馨提示").setMessage(message).setPositiveButton("確認", new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { result.confirm(); } }); b2.setCancelable(false); b2.create(); b2.show(); return true; }; public boolean onJsPrompt(WebView view, String url, final String message, String defaultValue, JsPromptResult result) { // webView.stopLoading(); // webView.reload(); // result.confirm(); //如果自己處理js 則最後必須result.confirm return false; }; };

openFileChooser方法寫這麼多個就是為了適配不同的版本,需要的朋友們,可以看看。