<i id='r4JHt'><tr id='r4JHt'><dt id='r4JHt'><q id='r4JHt'><span id='r4JHt'><b id='r4JHt'><form id='r4JHt'><ins id='r4JHt'></ins><ul id='r4JHt'></ul><sub id='r4JHt'></sub></form><legend id='r4JHt'></legend><bdo id='r4JHt'><pre id='r4JHt'><center id='r4JHt'></center></pre></bdo></b><th id='r4JHt'></th></span></q></dt></tr></i><div id='r4JHt'><tfoot id='r4JHt'></tfoot><dl id='r4JHt'><fieldset id='r4JHt'></fieldset></dl></div>

  • <legend id='r4JHt'><style id='r4JHt'><dir id='r4JHt'><q id='r4JHt'></q></dir></style></legend>

    <small id='r4JHt'></small><noframes id='r4JHt'>

  • <tfoot id='r4JHt'></tfoot>

        <bdo id='r4JHt'></bdo><ul id='r4JHt'></ul>
      1. WebView 中的文件上传

        时间:2023-08-29
      2. <small id='HCWrk'></small><noframes id='HCWrk'>

        <legend id='HCWrk'><style id='HCWrk'><dir id='HCWrk'><q id='HCWrk'></q></dir></style></legend>

      3. <i id='HCWrk'><tr id='HCWrk'><dt id='HCWrk'><q id='HCWrk'><span id='HCWrk'><b id='HCWrk'><form id='HCWrk'><ins id='HCWrk'></ins><ul id='HCWrk'></ul><sub id='HCWrk'></sub></form><legend id='HCWrk'></legend><bdo id='HCWrk'><pre id='HCWrk'><center id='HCWrk'></center></pre></bdo></b><th id='HCWrk'></th></span></q></dt></tr></i><div id='HCWrk'><tfoot id='HCWrk'></tfoot><dl id='HCWrk'><fieldset id='HCWrk'></fieldset></dl></div>
      4. <tfoot id='HCWrk'></tfoot>

                <tbody id='HCWrk'></tbody>
                <bdo id='HCWrk'></bdo><ul id='HCWrk'></ul>

                  本文介绍了WebView 中的文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I have been struggling to upload files from WebView since last few days and there is no progress. I googled and implemented all suggested solutions but none works, like: solutions suggested here, and so on.

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

                  Problem: I have a HTML page with the following code to upload a file. It works fine in a desktop browser like Firefox and built-in browser of emulator / AVD i.e., when I click "Browse..." button rendered by element, browser opens a Dialog box where I can choose a file to upload.

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

                  However, in the android 3.0 emulator / AVD, when I click on "Choose file", nothing happens, no file dialog is opened!!!

                  <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>
                  

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

                  推荐答案

                  这是一个适用于所有安卓版本的完整解决方案,我也遇到了困难.

                  This is a full solution for all android versions, I had a hard time with this too.

                  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上传,不带预览.

                  Also I want to add that the "upload page" like the one in this example, wont work on < 4 versions, since it has an image preview feature, if you want to make it work use a simple php upload without preview.

                  更新:

                  请在这里找到棒棒糖设备的解决方案 并感谢 鬼脸

                  更新 2:

                  在 oreo here 和 这是更高级的版本,你应该看看它,也许它可以帮助.

                  Complete solution for all android devices till oreo here and this is more advanced version, you should look into it, maybe it can help.

                  这篇关于WebView 中的文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么安卓模拟器这么慢?我们如何加快Android模拟 下一篇:如何将图像存储在 SQLite 数据库中

                  相关文章

                  最新文章

                  <tfoot id='0qGO8'></tfoot>

                    <legend id='0qGO8'><style id='0qGO8'><dir id='0qGO8'><q id='0qGO8'></q></dir></style></legend>
                      <bdo id='0qGO8'></bdo><ul id='0qGO8'></ul>

                    <small id='0qGO8'></small><noframes id='0qGO8'>

                      <i id='0qGO8'><tr id='0qGO8'><dt id='0qGO8'><q id='0qGO8'><span id='0qGO8'><b id='0qGO8'><form id='0qGO8'><ins id='0qGO8'></ins><ul id='0qGO8'></ul><sub id='0qGO8'></sub></form><legend id='0qGO8'></legend><bdo id='0qGO8'><pre id='0qGO8'><center id='0qGO8'></center></pre></bdo></b><th id='0qGO8'></th></span></q></dt></tr></i><div id='0qGO8'><tfoot id='0qGO8'></tfoot><dl id='0qGO8'><fieldset id='0qGO8'></fieldset></dl></div>