Android:讓WebView支援<input type=”file”…>元素

來源:互聯網
上載者:User

標籤:android   style   class   blog   code   java   

在Android中,當我們通過WebView開啟一個頁面時,如果裡面有元素是<input type=”file”…>類型的,WebView只能正常的顯示樣式,但是是無法點擊的。要解決這個問題,我們需要重寫WebChromeClient。

 

下面直接給出Demo代碼:

Activity檔案:

public class MainActivity extends Activity {    private final String host = "demo.com";    private final String urlAddress = "http://" + host;    private WebView web;    private 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;        }    }    /**     * Called when the activity is first created.     */    @SuppressLint("SetJavaScriptEnabled")    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        web = (WebView) findViewById(R.id.webView1);        progressBar = (ProgressBar) findViewById(R.id.progressBar1);        WebSettings settings = web.getSettings();        settings.setJavaScriptEnabled(true);        web.loadUrl(urlAddress);        web.setWebViewClient(new MyWebViewClient());        web.setWebChromeClient(new WebChromeClient() {            //關鍵代碼,以下函數是沒有API文檔的,所以在Eclipse中會報錯,如果添加了@Override關鍵字在這裡的話。            // 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/*");                MainActivity.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("*/*");                MainActivity.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/*");                MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE);            }        });//        setContentView(web);    }    private class MyWebViewClient extends WebViewClient {        @Override        public boolean shouldOverrideUrlLoading(WebView view, String url) {            if (Uri.parse(url).getHost().equals(host)) {                // This is my web site, so do not override; let my WebView load the page                return false;            }            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));            startActivity(intent);            return true;        }        @Override        public void onPageStarted(WebView view, String url, Bitmap favicon) {            // TODO Auto-generated method stub            super.onPageStarted(view, url, favicon);        }        @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);    }    // 捕捉“回退”按鍵,讓WebView能回退到上一頁,而不是直接關閉Activity。    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {            web.goBack();            return true;        }        return super.onKeyDown(keyCode, event);    }}

Layout代碼就不貼出來了,就是很簡單的一個WebView和一個Progress。通過以上代碼,我們就能夠在WebView中上傳檔案了。

 

PS: 和iOS比起來,這不是一個完美的解決方案,因為它不支援直接拍照上傳檔案,如果需要這個功能的話,那麼還需要做進一步的開發才行。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.