webview支援H5開啟並上傳圖片
阿新 • • 發佈:2019-01-03
mWebView.setWebChromeClient(new WebChromeClient() { // For Android 5.0+ @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { mUploadCallbackAboveL = filePathCallback; choseSinglePic(); // take(); return true; } // For Android 3.0+ public void openFileChooser(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; choseSinglePic(); } //3.0--版本 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { mUploadMessage = uploadMsg; choseSinglePic(); } // For Android 4.1 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { mUploadMessage = uploadMsg; choseSinglePic(); }
onActivityResult的處理
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage && null == mUploadCallbackAboveL) return; selectList = PictureSelector.obtainMultipleResult(data); Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); if (mUploadCallbackAboveL != null) { onActivityResultAboveL(requestCode, resultCode, data); } else if (mUploadMessage != null) { if (result != null) { String path = getPath(getActivity().getApplicationContext(), result); Uri uri = Uri.fromFile(new File(path)); mUploadMessage.onReceiveValue(uri); } else { mUploadMessage.onReceiveValue(imageUri); } mUploadMessage = null; } } } @SuppressWarnings("null") @TargetApi(Build.VERSION_CODES.BASE) private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) { if (requestCode != FILECHOOSER_RESULTCODE || mUploadCallbackAboveL == null) { return; } Uri[] results = null; if (data == null) { results = new Uri[]{imageUri}; Log.e("results==null", "results" + results.toString()); } else { selectList = PictureSelector.obtainMultipleResult(data); if (selectList != null && selectList.size() > 0) { results=new Uri[selectList.size()]; String path =""; for (int i = 0; i < selectList.size(); i++) { LocalMedia localMedia = selectList.get(i); if (localMedia.isCut() && !localMedia.isCompressed()) { // // 裁剪過 path = localMedia.getCutPath(); } else if (localMedia.isCompressed() || (localMedia.isCut() && localMedia.isCompressed())) { // 壓縮過,或者裁剪同時壓縮過,以最終壓縮過圖片為準 path = localMedia.getCompressPath(); } else { // 原圖 path = localMedia.getPath(); } Uri uri = Uri.fromFile(new File(path)); results[i]=uri; } } 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 (results != null) { mUploadCallbackAboveL.onReceiveValue(results); mUploadCallbackAboveL = null; } else { results = new Uri[]{imageUri}; mUploadCallbackAboveL.onReceiveValue(results); mUploadCallbackAboveL = null; } return; }