浏览文件在WebView中停止工作

时间:2013-12-14 14:47:11

标签: android file-upload webview webviewclient

我正在尝试创建一个Android应用程序来访问我创建的简单Sinatra网站。该网站允许用户上传照片。只需使用WebView,我就可以通过以下方式浏览手机中的文件。

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("http://10.0.2.2:4567");
    }

    @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;
    }

}

当我试图让应用程序更好时,我发现这段代码使应用程序看起来更好(并且可能更好)。

public class MainActivity extends Activity {


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


        myWebView= (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("http://10.0.2.2:4567");
        myWebView.setWebViewClient(new NewWebViewClient());

    }

    private class NewWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview,String url)
        {
            webview.loadUrl(url);
            return true;

        }
    }

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
        {
            myWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @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;
    }

}

现在问题是当我点击网页上的浏览按钮时,浏览菜单没有出现在我的屏幕上。实际上没有任何反应。我猜NewWebViewClient会产生这个问题但是怎么可能呢?有没有办法再次启用文件浏览?我必须手动添加它,因为我读到在早期的Android版本中它是必要的吗?

我的目标是API 18,但API 17也出现了同样的问题。

--------------------------------- EDIT ------------- ------------------

仍有同样的问题。 openFileChooser为旧API提供了解决方案,但不适用于API 18

    myWebView.setWebChromeClient(new WebChromeClient() 

  {   
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {  

  mUploadMessage = uploadMsg;  
  Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
  i.addCategory(Intent.CATEGORY_OPENABLE);  
  i.setType("image/*");  
  Intent chooserIntent = Intent.createChooser(i,"Image Chooser");

  MainActivity16.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);  

         }  
  });  

  setContentView(myWebView);  
 }

1 个答案:

答案 0 :(得分:0)

https://stackoverflow.com/a/15454070/2394252

毕竟它与API有关。 Vorrtex在上面的链接中给出了解决方案。 就我而言,这是最终的代码。

    public class MainActivity extends Activity {

        WebView myWebView ;
        private ValueCallback<Uri> mUploadMessage;
        private final static int FILECHOOSER_RESULTCODE = 1;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            myWebView = new WebView(this);
            myWebView.setWebViewClient(new WebViewClient());
            myWebView.setWebChromeClient(new WebChromeClient() {
                //The undocumented magic method override  
                //Eclipse will swear at you if you try to put @Override here  
                public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                    MainActivity.this.showAttachmentDialog(uploadMsg);
                }

                // For Android > 3.x
                public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                    MainActivity.this.showAttachmentDialog(uploadMsg);
                }

                // For Android > 4.1
                public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                    MainActivity.this.showAttachmentDialog(uploadMsg);
                }
            });

            this.setContentView(myWebView);

            myWebView.loadUrl("http://10.0.2.2:4567");

        }

        private void showAttachmentDialog(ValueCallback<Uri> uploadMsg) {
            this.mUploadMessage = uploadMsg;

            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");

            this.startActivityForResult(Intent.createChooser(i, "Choose type of attachment"), FILECHOOSER_RESULTCODE);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            if (requestCode == FILECHOOSER_RESULTCODE) {
                if (null == this.mUploadMessage) {
                    return;
                }
                Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
                this.mUploadMessage.onReceiveValue(result);
                this.mUploadMessage = null;
            }
        }

        @Override 
        public boolean onKeyDown(int keyCode, KeyEvent event)
        {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
            {
                myWebView.goBack();
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }

        @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;
        }
}

不幸的是,由于openFileChooser不可用,目前没有kitkat的解决方案