android - 在 WebView 中打开文件上传菜单不起作用

标签 android webview android-webview webchromeclient

我需要能够在 Web View 中从 AndroidAddMember.aspx 页面打开文件上传菜单。我找到了此修复程序,但我认为我没有正确实现它。 Fix

文件上传按钮在手机和电脑的浏览器中工作正常,但一旦在 WebView 中该按钮不起作用。

我在下面的第一个案例陈述中嵌入了带有按钮的页面。

任何帮助都会很棒。谢谢

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

}    
}    

@Override
public boolean onOptionsItemSelected(MenuItem item) {    
    // Handle item selection    
    switch (item.getItemId()) {        
    case R.id.register:            

        wv = new WebView(this);  
        wv.setWebViewClient(new WebViewClient());   
        wv.getSettings().setJavaScriptEnabled(true); 

        wv.loadUrl("http://www.mysite.com/AndroidAddMember.aspx"); 

          wv.setWebViewClient(new WebViewClient());    
          wv.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) {    

                  mUploadMessage = uploadMsg;    
                  Intent i = new Intent(Intent.ACTION_GET_CONTENT);    
                  i.addCategory(Intent.CATEGORY_OPENABLE);    
                  i.setType("image/*");    
                  Myactivity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);    

                 }    
        });    
            setContentView(wv);    


                 return true;
    default:            
        return super.onOptionsItemSelected(item);    
        }
}

最佳答案

实际上,webview 不允许以这种方式上传文件。您必须在应用程序的菜单中创建一个上传按钮。并把文件输入然后通过http上传到你的网站。

或者更好的是,您应该使用 phonegap 使其成为可能。在手机间隙,您可以轻松使用文件上传和下载。

http://docs.phonegap.com/en/2.1.0/cordova_file_file.md.html#File

只有这样,您才能将文件上传到您的服务器,否则就无法上传。 Webview 仅提供基本功能。

你最好用这种方式。

关于android - 在 WebView 中打开文件上传菜单不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12674179/

相关文章:

java - 如何: Add to Favourites and View the Favourites list

android - WebView + Blob URL + 下载图片

android - 改变 WebView 中显示的数据

android - 如何在android webview中使用弹出窗口

android-webview - 带证书的 Android 4.0 Webview SSL 身份验证

android - 如何在 Android 上获取 parent ID?

android - 在 React Native 中获取设备 ID?

android - 在android中屏幕旋转时如何保留文本选择状态

java - 从通知启动 Activity 并在 BackPressed 上传递数据

javascript - 如何从WinJS/javascript使用WebView控件navigateToLocalStreamUri