Android WebView中打开相机拍照和选择相册
生活随笔
收集整理的這篇文章主要介紹了
Android WebView中打开相机拍照和选择相册
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一般在項(xiàng)目中與js交互,可能會(huì)遇到上傳文件圖片等操作,避免不了一些坑,下面簡單說一下,Android 在不同版本中webView調(diào)用相機(jī),選擇相冊的方法是不一樣的,3.0以下的調(diào)用
public void openFileChooser(ValueCallback<Uri> uploadMsg)3.0以上: public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)4.4以下: public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
5.0以上: public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams)
那下面具體的貼下代碼:
繼承自WebChromeClient,重寫
//3.0++ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {openFileChooserImpl(uploadMsg); }//3.0-- public void openFileChooser(ValueCallback<Uri> uploadMsg) {openFileChooserImpl(uploadMsg); }//4.1 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {openFileChooserImpl(uploadMsg); }@Override public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {openFileChooserImplForAndroid5(filePathCallback); return true; }
外層定義接收返回值:
private ValueCallback<Uri> mUploadMessage; private ValueCallback<Uri[]> mUploadCallbackAboveL;不同的版本調(diào)不同的方法:
private void openFileChooserImplForAndroid5(ValueCallback<Uri[]> uploadMsg) {mUploadCallbackAboveL = uploadMsg; dispatchTakePictureIntent(); }//5.0以下的掉用 private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) {mUploadMessage = uploadMsg; dispatchTakePictureIntent(); }//拍照 private void dispatchTakePictureIntent() {selectImgDialog(); }
我下面就把代碼全部貼出來吧:
private void takePhoto() {Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {Uri imageUri = null; try {imageUri = Uri.fromFile(createImageFile()); } catch (IOException e) {e.printStackTrace(); }takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(takePictureIntent, FILECHOOSER_RESULTCODE); }}/** * 209. * 本地相冊選擇圖片 * 210. */ private void chosePic() {Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); String IMAGE_UNSPECIFIED = "image/*"; innerIntent.setType(IMAGE_UNSPECIFIED); // 查看類型 Intent wrapperIntent = Intent.createChooser(innerIntent, null); startActivityForResult(wrapperIntent, REQ_CHOOSE); }String mCurrentPhotoPath = null; String FileName = "forum"; //創(chuàng)建文件夾包裝圖片 private File createImageFile() throws IOException {File storageDir = new File(Util.getAppPath(getActivity()) + FileName); if (!storageDir.exists()) {storageDir.mkdirs(); }storageDir = new File(Util.getAppPath(getActivity()) + FileName + "/", System.currentTimeMillis() + ".jpg"); //保存當(dāng)前圖片路徑 mCurrentPhotoPath = storageDir.getAbsolutePath(); return storageDir; }//onActivityResult回調(diào) @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data); if (requestCode == FILECHOOSER_RESULTCODE || requestCode == REQ_CHOOSE) {if (null == mUploadMessage && null == mUploadCallbackAboveL) return; Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); if (mUploadCallbackAboveL != null) {onActivityResultAboveL(requestCode, data); } else if (mUploadMessage != null) {mUploadMessage.onReceiveValue(result); mUploadMessage = null; }}}//5.0以上版本,由于api不一樣,要單獨(dú)處理 // @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void onActivityResultAboveL(int requestCode, Intent data) {if (mUploadCallbackAboveL == null) {return; }Uri result = null; if (requestCode == FILECHOOSER_RESULTCODE) {File file = new File(mCurrentPhotoPath); Uri localUri = Uri.fromFile(file); Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri); getActivity().sendBroadcast(localIntent); result = Uri.fromFile(file); } else if (requestCode == REQ_CHOOSE) {result = data.getData(); }mUploadCallbackAboveL.onReceiveValue(new Uri[]{result}); mUploadCallbackAboveL = null; return; }后補(bǔ)的代碼:
private void openFileChooserImplForAndroid5(ValueCallback<Uri[]> uploadMsg) {mUploadCallbackAboveL = uploadMsg; dispatchTakePictureIntent(); }//5.0以下的掉用 private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) {mUploadMessage = uploadMsg; dispatchTakePictureIntent(); }
基本代碼就是以上這些,selectImgDialog()方法是彈窗提示選擇,關(guān)于這個(gè)彈窗布局的用法,之前寫的一篇文章說的很清楚了(BottomSheet 的詳解及注意事項(xiàng)),有疑問的歡迎提問。
總結(jié)
以上是生活随笔為你收集整理的Android WebView中打开相机拍照和选择相册的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Cloud kuberne
- 下一篇: shift后门