Android:讓WebView支持元素

最近在作一個活動頁面:用戶上傳一張圖片進行縮放、旋轉後點擊下一步填寫內容後生成圖片!html

作好後通過各類測試是沒有問題的,基本沒有什麼明顯BUG,流程都能走通,可是嵌入到APP後,問題就來了!web

在IOS上基本還能夠,在Android上有明顯問題,下面就是我要講的:ide

在Android中,當咱們經過WebView打開一個頁面時,若是裏面有元素是<input type=」file」…>類型的,WebView只能正常的顯示樣式,可是是沒法點擊的。要解決這個問題,咱們須要重寫WebChromeClient。函數

下面直接給出Demo代碼:測試

Activity文件:ui

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中上傳文件了。this

 

PS: 和iOS比起來,這不是一個完美的解決方案,由於它不支持直接拍照上傳文件,若是須要這個功能的話,那麼還須要作進一步的開發才行。url

原文地址:Android:讓WebView支持input: file元素spa

相關文章
相關標籤/搜索