文件上传中的WebView [英] File Upload in WebView

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

问题描述

我一直在努力,因为过去几年的的WebView上传文件 天,没有进展。我用Google搜索,并实施了所有建议 解决方案,但没有工作,如:解决方案建议在这里 - Android的web视图,文件输入字段文件选择器不会显示出来。 ... <一href="http://blog.tourizo.com/2009/02/how-to-display-local-file-in-android">http://blog.tourizo.com/2009/02/how-to-display-local-file-in-android.... 等。

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 - Android webview, file input field filechooser doesn't show up...., http://blog.tourizo.com/2009/02/how-to-display-local-file-in-android.... and so on.

问题:我有一个HTML页面下面的code要上传的文件。 它工作正常,在如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>

任何人都可以请建议在最早的一个可能的解决方案。

推荐答案

这是对所有的Andr​​oid版本的完整的解决方案,我的日子不好过这个了。

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);
}*/
}

另外我想补充一点,上传页面像在这个例子中,不会与LT工作; 4个版本,因为它有一个像preVIEW功能,如果你想它的工作使用一个简单的PHP上传没有preVIEW。

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.

更新

请找到解决方案的棒棒糖设备的<一个href="https://github.com/GoogleChrome/chromium-webview-samples/tree/master/input-file-example">here并感谢<一href="https://gauntface.com/blog/2014/10/17/what-you-need-to-know-about-the-webview-in-l">gauntface

Please find the solution for lollipop devices here and thanks for gauntface

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆