使用android web视图打开文件

时间:2017-06-01 04:25:27

标签: java android file webview android-webview

我使用android WebView创建了一个android应用程序,这里是我用来加载WebView的代码

WebView browser = (WebView) findViewById(R.id.webview);
    browser.loadUrl("http://samanretail.website/shv");
    browser.getSettings().setJavaScriptEnabled(true);
    browser.setWebViewClient(new WebViewClient());

问题是我无法访问网页浏览中的打开文件选项。当我点击打开文件选项时没有任何反应..请帮助我找到解决方案。

1 个答案:

答案 0 :(得分:0)

在点击事件

上尝试此代码
WebView web = (WebView) findViewById(R.id.webview);

web = new WebView(this);  
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.yoururl.com/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);