Web视图filechooser崩溃问题

时间:2017-12-02 04:49:33

标签: java android file android-webview

在onShowFileChooser (WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams)函数中,filePath为null;

Device configuration:
Debug-infos:
Android version: 21
Device: samsung SM-N9005
OS Version: 3.4.0-6408191(N9005DXSGBQA1)
Device: hlte

在其他设备和api级别,它运行正常。

webView.setWebChromeClient(new WebChromeClient() {

        // For Android 5.0
        public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams) {

            //qrCodeReaderView.stopCamera();
            mFilePathCallback = filePath;
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

            mCapturedImageURI = Uri.fromFile(photoFile);
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI
                );
            } else {

            }
            startActivityForResult(takePictureIntent, INPUT_FILE_REQUEST_CODE);

            return true;

        }

    });

关于活动结果:

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data);       
     mFilePathCallback.onReceiveValue(new Uri[]{mCapturedImageURI});   
     mFilePathCallback = null;
   }

logcat的:

java.lang.RuntimeException: 
Unable to resume activity   
{com.gaintz/com.shashank.gaintz.WebViewActivity}:  
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity  {com.gaintz/com.shashank.gaintz.WebViewActivity}: 

java.lang.NullPointerException: Attempt to invoke interface method 'void android.webkit.ValueCallback.onReceiveValue(java.lang.Object‌​)' on a null object reference at android.app.ActivityThread.performResumeActivity(ActivityThr‌​ead.java:3390) at 

1 个答案:

答案 0 :(得分:0)

我知道现在留下答案为时已晚,但是我遇到了同样的问题,因此我解决了这个问题,所以给以后的读者留下答案。

我仅在Android Lollipop(三星银河s4,note3)上遇到过这个问题。

每当我拍照时,filePath始终为空。

通过许多测试,我意识到每次拍照时Webview都会刷新。

它被刷新的原因是“活动导向”。

在Menifest中添加此项,以保留具有网络视图的活动的原始状态。

<activity 
 ...
 android:configChanges="orientation|screenSize">

您也可以参考Android - Preventing WebView reload on Rotate