android - WebView 中的文件上传

标签 android android-emulator webview

自从最近几次以来,我一直在努力从 WebView 上传文件 几天了,一点进展都没有。我用谷歌搜索并实现了所有建议 解决方案,但没有一个有效,例如:建议的解决方案here , 等等。

问题:我有一个带有以下代码的 HTML 页面来上传文件。 它在 Firefox 等桌面浏览器和内置浏览器中运行良好 模拟器/AVD,即当我点击“浏览...”按钮呈现 元素,浏览器打开一个对话框 我可以在其中选择要上传的文件的框。

但是,在 android 3.0 模拟器/AVD 中,当我点击“选择 文件”,没有任何反应,没有打开文件对话框!!!

<form method="POST" enctype="multipart/form-data">
File to upload: <input type="file" name="uploadfile">&nbsp;&nbsp;
<input type="submit" value="Press to Upload..."> to upload the file!
</form>

谁能尽早提出一个可能的解决方案。

最佳答案

这是适用于所有 android 版本的完整解决方案,我也遇到了困难。

public class MyWb extends Activity {
/** Called when the activity is first created. */

WebView web;
ProgressBar progressBar;

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

 @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 void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    web = (WebView) findViewById(R.id.webview01);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    web = new WebView(this);  
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("http://www.script-tutorials.com/demos/199/index.html");
    web.setWebViewClient(new myWebClient());
    web.setWebChromeClient(new WebChromeClient()  
    {  
           //The undocumented magic method override  
           //Eclipse will swear at you if you try to put @Override here  
        // For Android 3.0+
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {  

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

           }

        // For Android 3.0+
           public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
           mUploadMessage = uploadMsg;
           Intent i = new Intent(Intent.ACTION_GET_CONTENT);
           i.addCategory(Intent.CATEGORY_OPENABLE);
           i.setType("*/*");
           MyWb.this.startActivityForResult(
           Intent.createChooser(i, "File Browser"),
           FILECHOOSER_RESULTCODE);
           }

        //For Android 4.1
           public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
               mUploadMessage = uploadMsg;  
               Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
               i.addCategory(Intent.CATEGORY_OPENABLE);  
               i.setType("image/*");  
               MyWb.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MyWb.FILECHOOSER_RESULTCODE );

           }

    });  


    setContentView(web);  


}

public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub

        view.loadUrl(url);
        return true;

    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

        progressBar.setVisibility(View.GONE);
    }
}

//flipscreen not loading again
@Override
public void onConfigurationChanged(Configuration newConfig){        
    super.onConfigurationChanged(newConfig);
}

// To handle "Back" key press event for WebView to go back to previous screen.
/*@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
        web.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}*/
}

我还想补充一点,像这个例子中的“上传页面”,不能在 < 4 个版本上工作,因为它有一个图像预览功能,如果你想让它工作,使用一个简单的 php 上传而不预览.

更新:

请查找 Lollipop 设备的解决方案here感谢 gauntface

更新 2:

直到oreo here的所有安卓设备的完整解决方案和 this is more advanced version ,你应该看看它,也许它可以帮助。

关于android - WebView 中的文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5907369/

相关文章:

android - 谷歌移动视觉不适用于 Yuv 数据?

android - ClassCastException : NoClassDefFoundError cannot be cast to RuntimeException

android - 改进 Windows 7 x64 上的 Android 模拟器性能

android - 如何从AccessibilityNodeInfo获取webview

android - 在 Android WebView 中播放嵌入 HTML 中的 youtube/vimeo 视频

android - 动画期间点击 View 没有反应

android - 安装后自动将小部件放在桌面上

android - 关于在 Android 2.1 中使用 Fragment 更新

android - 如何在android模拟器中增加sd卡内存?

windows-8 - WinRt WebView 控件处理控件内的导航