返回onactivityResult按钮时WebView不起作用

时间:2014-02-14 10:03:43

标签: android button android-intent android-webview

我有包含webview的MainActivity,我在内部加载了我在assets文件夹中的html文件。 这个HTML看起来像这样:(目前非常简单)

<html>
    <body>
        <form enctype="multipart/form-data" method="post">    
              <h2>capture=camera</h2>  
              <input type="file" accept="image/*;capture=camera"></input>
        </form>
    </body>
</html>

单击html中的按钮时,会启动CameraActivity,返回图像的Uri。

这就是问题所在。因为在MainActivity中,在返回时,webview加载正常,但是,里面的按钮不再响应。

MainAcitivy:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setWebView();
        startWebView();

        Log.d("CAMERA", "UNO");
    }


    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        setWebView();
        startWebView();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        setWebView();
        startWebView();
    }

    private void setWebView() {
        webview = (WebView) findViewById(R.id.webView1);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.getSettings().setAllowFileAccess(true);

        webview.loadUrl(URL);
    }
    private void startWebView() {

        webview.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(URL);
                return true;

            }
        });


        webview.setWebChromeClient(new WebChromeClient() {


            public void openFileChooser(ValueCallback<Uri> uploadMsg,String acceptType) {

                mUploadMessage = uploadMsg;

                try {
                    Intent intent= new Intent(MainActivity.this, CameraActivity.class);
                    startActivityForResult(intent,CAM_ACT);

                } catch (Exception e) {
                    Toast.makeText(getBaseContext(), "Exception:" + e,Toast.LENGTH_LONG).show();
                }

            }

            // openFileChooser for Android < 3.0
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                openFileChooser(uploadMsg, "");
            }

            // openFileChooser for other Android versions
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {

                openFileChooser(uploadMsg, acceptType);
            }

        }); // End setWebChromeClient



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == CAM_ACT) {
            Log.d("Camera", "Activity result -->" + data.getExtras().getString("result"));
        }
        webview = null;
        setWebView();
        startWebView();
    }

当我再次单击该按钮时,Log会返回此消息:

02-14 11:01:40.048: V/WebViewInputDispatcher(16553): blockWebkitDraw
02-14 11:01:40.048: V/WebViewInputDispatcher(16553): blockWebkitDraw lockedfalse

我做错了什么?我该如何解决?

谢谢大家。


编辑解决方案

我的问题的解决方案以这种方式解决了(由@ksasq提供) 在“OnActivityResult”中我必须添加mUploadMessage的行来解决它

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        // webview.loadUrl(URL);
        if (requestCode == CAM_ACT) {
            Log.d("Camera", "Activity result -->" + data.getExtras().getString("result"));
            Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
            this.mUploadMessage.onReceiveValue(result);
            this.mUploadMessage = null;
        }


        setWebView();
        startWebView();
    }

1 个答案:

答案 0 :(得分:1)

当您在onActivityResult中收到文件时,您需要将传递到openFileChooser的uploadMessage发送回WebKit。类似的东西:

uploadMsg.receiveValue(data.getExtras().getString("result"))

请注意,文件选择器方法是@hidden,而不是公共Android SDK的一部分。将来可能不会支持它们。实际上,它们并不适用于Android 4.4,并且没有相关的解决方法。