美文网首页Android开发经验谈程序员Android知识
WebView调起系统相册和拍照功能

WebView调起系统相册和拍照功能

作者: xbase | 来源:发表于2018-01-18 16:41 被阅读88次

    公司项目,需要在webview中调起相册和拍照,网上文章照搬照抄一大堆,一个都不好使,系统兼容性,品牌兼容性,问题多多,我直接上代码:
    重写WebViewChromeClient中的方法:

    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                openFileChooserImpl(uploadMsg);
            }
    
            //3.0--版本
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                openFileChooserImpl(uploadMsg);
            }
    
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                openFileChooserImpl(uploadMsg);
            }
    
            // For Android > 5.0
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
                openFileChooserImplForAndroid5(uploadMsg);
                return true;
            }
    

    根据系统不同调用不同方法,如下:

    private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) {
            mUploadMessage = uploadMsg;
            Intent chooserIntent = IntentUtil.createImageChooserIntent(getString(R.string.carowner_register_select_image_source));
            startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
        }
    
        private void openFileChooserImplForAndroid5(ValueCallback<Uri[]> uploadMsg) {
            mUploadCallbackAboveL = uploadMsg;
            Intent chooserIntent = IntentUtil.createImageChooserIntent(getString(R.string.carowner_register_select_image_source));
            startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5);
        }
    

    接下来是IntentUtil中的方法:

    /**
       * 返回获取图片Intent,包含相册选择和拍照二项
       * @param title picker标题
       * @return intent
       */
      public static Intent createImageChooserIntent(final String title) {
        Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType("image/*");
    
        Intent galleryIntent = new Intent(
                Intent.ACTION_GET_CONTENT,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    
        Intent takePhotoIntent = takePhoto();
    
        Intent chooserIntent = Intent.createChooser(galleryIntent, title);
        chooserIntent.putExtra
                (
                        Intent.EXTRA_INITIAL_INTENTS,
                        new Intent[]{takePhotoIntent}
                );
        return chooserIntent;
      }
    
      static Uri imageUri;
      private static Intent takePhoto() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        try {
          if (Build.VERSION.SDK_INT >= 24) {
            imageUri = FileProvider.getUriForFile(BaseApplication.getApplication(),
                    "com.yongche.provider",
                    createImageFile());
          } else {
            imageUri = Uri.fromFile(createImageFile());
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (imageUri != null) {
          takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        }
        return takePictureIntent;
      }
    
      /**
       * 拍照后创建临时文件,供上传使用
       * @return
       * @throws IOException
       */
      private static File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = BaseApplication.getApplication().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );
    
        return image;
      }
    
      public static Uri getImageUri() {
        return imageUri;
      }
    

    最后在调用的Activity中重写OnActivityResult方法:

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
            if (requestCode == FILECHOOSER_RESULTCODE) {
                if (null == mUploadMessage)
                    return;
    
                if (resultCode == RESULT_OK) {
                    Uri result = (data == null) ? null : data.getData();
                    if (result != null) {
                        mUploadMessage.onReceiveValue(result);
                    } else {
                        Uri imageUri = IntentUtil.getImageUri();
                        if (imageUri != null) {
                            mUploadMessage.onReceiveValue(imageUri);
                        }
                    }
                }else if (resultCode == RESULT_CANCELED) {
                    mUploadMessage.onReceiveValue(null);
                }
    
                mUploadMessage = null;
    
            } else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5) {
                if (null == mUploadCallbackAboveL)
                    return;
                if (resultCode == RESULT_OK) {
                    Uri result = (data == null) ? null : data.getData();
                    if (result != null) {
                        mUploadCallbackAboveL.onReceiveValue(new Uri[]{result});
                    } else {
                        Uri imageUri = IntentUtil.getImageUri();
                        if (imageUri != null) {
                            mUploadCallbackAboveL.onReceiveValue(new Uri[]{imageUri});
                        }
                    }
                } else if (resultCode == RESULT_CANCELED) {
                    mUploadCallbackAboveL.onReceiveValue(new Uri[]{});
                }
                mUploadCallbackAboveL = null;
            }
    
            super.onActivityResult(requestCode, resultCode, data);
    
        }
    

    相关文章

      网友评论

        本文标题:WebView调起系统相册和拍照功能

        本文链接:https://www.haomeiwen.com/subject/qfyuoxtx.html