android webview中的相机onShowFileChooser()仅工作一次

时间:2019-02-13 08:59:54

标签: android webview android-camera android-6.0-marshmallow filechooser

我需要从摄像头webview android拍照,在oreo和pie上一切都很好,但是在棉花糖中却不行,当我在棉花糖中从相机拍照时,它只能在第一次使用时

camera

,仅当拍照后,我单击“删除”按钮,如上面的屏幕截图,然后再次拍照,然后单击右键,它可以工作,并且可以上载图片,但如果再次拍照但是我没有点击删除按钮,而是点击了右键,因此无法上传图片。我知道这与来自activityresult的onReceiveValue有关,并且我从here!找到了解决方案,但是我不知道如何在下面的代码中实现它。拜托,有人可以帮我吗?我真的需要帮助,谢谢。

private String mCM;
    private ValueCallback mUM;
    private ValueCallback<Uri[]> mUMA;
    private final static int FCR=1;
    private Uri mCapturedImageURI = null;

这是onActivityResult()

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){
        if(Build.VERSION.SDK_INT >= 21){
            if (requestCode != FCR || mUMA == null) {
                super.onActivityResult(requestCode, resultCode, intent);
                return;
            }
            Uri[] results = null;
            //Check if response is positive
            if(resultCode== Activity.RESULT_OK){
                if(requestCode == FCR){
                    if(null == mUMA){
                        return;
                    }
                    if(intent == null || intent.getData() == null){
                        //Capture Photo if no image available
                        if(mCM != null){
                            results = new Uri[]{Uri.parse(mCM)};
                        }else{
                            results = new Uri[]{};
                        }
                    }else{
                        String dataString = intent.getDataString();
                        if(dataString != null){
                            results = new Uri[]{Uri.parse(dataString)};
                        }else{
                            results = new Uri[]{};
                        }
                    }
                }
            }
            mUMA.onReceiveValue(results);
            mUMA = null;
        }else{
            if (requestCode != FCR || mUM == null) {
                super.onActivityResult(requestCode, resultCode, intent);
                return;
            }
            if (requestCode == FCR) {
                if (null == this.mUM) {
                    return;
                }
                Uri result = null;
                try {
                    if (resultCode != RESULT_OK) {
                        result = null;
                    } else {
                        // retrieve from the private variable if the intent is null
                        result = intent == null ? mCapturedImageURI : intent.getData();
                    }
                } catch (Exception e) {
                }
                mUM.onReceiveValue(result);
                mUM = null;
            }
        }
    }

这是onShowFileChooser()

 webView.setWebChromeClient(new WebChromeClient(){
                public boolean onShowFileChooser(
                        WebView webView, ValueCallback<Uri[]> filePathCallback,
                        FileChooserParams fileChooserParams){
                    if(Build.VERSION.SDK_INT >= 21) {
                        checkPermissions();
                    }
                    if(mUMA != null){
                        mUMA.onReceiveValue(null);
                    }
                    mUMA = filePathCallback;
                    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if(takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null){
                        File photoFile = null;
                        try{
                            photoFile = createImageFile();
                            takePictureIntent.putExtra("PhotoPath", mCM);
                        }catch(IOException ex){
                            Log.e(TAG, "Image file creation failed", ex);
                        }
                        if(photoFile != null){
                            mCM = "file:" + photoFile.getAbsolutePath();
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                        }else{
                            takePictureIntent = null;
                        }
                    }
                    Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
                    contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
                    contentSelectionIntent.setType("image/*");
                    Intent[] intentArray;
                    if(takePictureIntent != null){
                        intentArray = new Intent[]{takePictureIntent};
                    }else{
                        intentArray = new Intent[0];
                    }

                    Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
                    chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
                    chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
                    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
                    startActivityForResult(chooserIntent, FCR);
                    return true;
                }
            });

0 个答案:

没有答案