Android WebView 不支持 H5 input type="file" 解決方法
最近因為趕項目進度,因此將本來要用原生控件實現(xiàn)的界面,自己做了H5并嵌入webview中。發(fā)現(xiàn)點擊H5中 input type="file" 標(biāo)簽 不能打開android資源管理器。
通過網(wǎng)絡(luò)搜索發(fā)現(xiàn)是因為 android webview 由于考慮安全原因屏蔽了 input type="file" 這個功能 。
經(jīng)過不懈的努力,以及google 翻譯的幫助 在 stackoverflow 中找到了解決的方法。
具體可以理解為 重寫webview 的WebChromeClient ,廢話不多說直接貼代碼:
private ValueCallback<Uri> mUploadMessage; public ValueCallback<Uri[]> uploadMessage; public static final int REQUEST_SELECT_FILE = 100; private final static int FILECHOOSER_RESULTCODE = 2; webview.setWebChromeClient(new WebChromeClient(){ // For 3.0+ Devices (Start) // onActivityResult attached before constructor protected void openFileChooser(ValueCallback uploadMsg, String acceptType) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE); } // For Lollipop 5.0+ Devices @TargetApi(Build.VERSION_CODES.LOLLIPOP) public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { if (uploadMessage != null) { uploadMessage.onReceiveValue(null); uploadMessage = null; } uploadMessage = filePathCallback; Intent intent = fileChooserParams.createIntent(); try { startActivityForResult(intent, REQUEST_SELECT_FILE); } catch (ActivityNotFoundException e) { uploadMessage = null; Toast.makeText(getBaseContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show(); return false; } return true; } //For Android 4.1 only protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { mUploadMessage = uploadMsg; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE); } protected void openFileChooser(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (requestCode == REQUEST_SELECT_FILE) { if (uploadMessage == null) return; uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent)); uploadMessage = null; } } else if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; // Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment // Use RESULT_OK only if you're implementing WebView inside an Activity Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } else Toast.makeText(getBaseContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show(); }
以上所述是小編給大家介紹的Android WebView 不支持 H5 input type="file" 解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android中的windowSoftInputMode屬性詳解
- android 使用uinput模擬輸入設(shè)備的方法
- Android 數(shù)據(jù)存儲之 FileInputStream 工具類及FileInputStream類的使用
- Android編程開發(fā)之EditText中inputType屬性小結(jié)
- Android WebView支持input file啟用相機/選取照片功能
- Android網(wǎng)頁H5 Input選擇相機和系統(tǒng)相冊
- 詳解Android WebView的input上傳照片的兼容問題
- Android自定義PasswordInputView密碼輸入
- Android InputMethodManager輸入法簡介
- 從"Show?tabs"了解Android?Input系統(tǒng)
相關(guān)文章
Android 自定義View時使用TypedArray配置樣式屬性詳細介紹
這篇文章主要介紹了Android 自定義View時使用TypedArray配置樣式屬性詳細介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11Android開發(fā)之TextView使用intent傳遞信息,實現(xiàn)注冊界面功能示例
這篇文章主要介紹了Android開發(fā)之TextView使用intent傳遞信息,實現(xiàn)注冊界面功能,涉及Android使用intent傳值及界面布局等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04Android SwipeRefreshLayout仿抖音app靜態(tài)刷新
這篇文章主要為大家詳細介紹了Android SwipeRefreshLayout仿抖音app靜態(tài)刷新,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03Android?實現(xiàn)單指滑動雙指縮放照片demo及過程解析
這篇文章主要為大家介紹了Android?實現(xiàn)單指滑動雙指縮放照片demo及過程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04