最近在做一個活動頁面:用戶上傳一張圖片進行縮放、旋轉後點擊下一步填寫內容後生成圖片! 做好後經過各種測試是沒有問題的,基本沒有什麼明顯BUG,流程都能走通,但是嵌入到APP後,問題就來了! 在IOS上基本還可以,在Android上有明顯問題,下麵就是我要講的: 在Android中,當我們通過WebV ...
最近在做一個活動頁面:用戶上傳一張圖片進行縮放、旋轉後點擊下一步填寫內容後生成圖片!
做好後經過各種測試是沒有問題的,基本沒有什麼明顯BUG,流程都能走通,但是嵌入到APP後,問題就來了!
在IOS上基本還可以,在Android上有明顯問題,下麵就是我要講的:
在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比起來,這不是一個完美的解決方案,因為它不支持直接拍照上傳文件,如果需要這個功能的話,那麼還需要做進一步的開發才行。
原文地址:Android:讓WebView支持input: file元素