Android WebView 不支持 H5 input type="file" 解決方法
最近因?yàn)橼s項(xiàng)目進(jìn)度,因此將本來(lái)要用原生控件實(shí)現(xiàn)的界面,自己做了H5并嵌入webview中。發(fā)現(xiàn)點(diǎn)擊H5中 input type="file" 標(biāo)簽 不能打開(kāi)android資源管理器。
通過(guò)網(wǎng)絡(luò)搜索發(fā)現(xiàn)是因?yàn)?android webview 由于考慮安全原因屏蔽了 input type="file" 這個(gè)功能 。
經(jīng)過(guò)不懈的努力,以及google 翻譯的幫助 在 stackoverflow 中找到了解決的方法。
具體可以理解為 重寫webview 的WebChromeClient ,廢話不多說(shuō)直接貼代碼:
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" 解決方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android中的windowSoftInputMode屬性詳解
- android 使用uinput模擬輸入設(shè)備的方法
- Android 數(shù)據(jù)存儲(chǔ)之 FileInputStream 工具類及FileInputStream類的使用
- Android編程開(kāi)發(fā)之EditText中inputType屬性小結(jié)
- Android WebView支持input file啟用相機(jī)/選取照片功能
- Android網(wǎng)頁(yè)H5 Input選擇相機(jī)和系統(tǒng)相冊(cè)
- 詳解Android WebView的input上傳照片的兼容問(wèn)題
- Android自定義PasswordInputView密碼輸入
- Android InputMethodManager輸入法簡(jiǎn)介
- 從"Show?tabs"了解Android?Input系統(tǒng)
相關(guān)文章
Android實(shí)現(xiàn)消息總線的幾種方式詳解
關(guān)于Android消息傳遞方式比較多,一般的系統(tǒng)原生實(shí)現(xiàn)方式比如Handler?、自定義廣播、接口回調(diào),以及三方工具?EventBus?、RxBus?等,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)消息總線的幾種方式,需要的朋友可以參考下2022-06-06
Android實(shí)現(xiàn)顯示和隱藏密碼功能的示例代碼
在前端中我們知道用javascript就可以可以很容易實(shí)現(xiàn)密碼的顯示與隱藏,本文將大家詳細(xì)介紹Android是如何實(shí)現(xiàn)顯示和隱藏密碼功能的,需要的可以參考一下2022-06-06
Android Studio 3.1.X中導(dǎo)入項(xiàng)目的正確方法分享
這篇文章主要給大家介紹了關(guān)于Android Studio 3.1.X中導(dǎo)入項(xiàng)目的正確方法,文中一步步將解決的方法以及可能遇到的問(wèn)題介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Android實(shí)現(xiàn)登錄注冊(cè)頁(yè)面(上)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)登錄注冊(cè)頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
輕松實(shí)現(xiàn)功能強(qiáng)大的Android刮獎(jiǎng)效果控件(ScratchView)
這篇文章主要為大家詳細(xì)介紹了ScratchView如何一步步打造萬(wàn)能的Android刮獎(jiǎng)效果控件,,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
uniapp打包Android的apk(原生APP-云打包)及發(fā)布測(cè)試全過(guò)程
uni-app本地打包apk需要提前做非常多的準(zhǔn)備工作,而且可能會(huì)勸退一些開(kāi)發(fā)者,下面這篇文章主要給大家介紹了關(guān)于uniapp打包Android的apk(原生APP-云打包)及發(fā)布測(cè)試的相關(guān)資料,需要的朋友可以參考下2023-02-02
Android Handler 機(jī)制實(shí)現(xiàn)原理分析
本文主要介紹 Android Handle機(jī)制實(shí)現(xiàn)的原理,這里整理了詳細(xì)的關(guān)于Handler的資料以及工作流程和實(shí)際應(yīng)用,有興趣的小伙伴可以參考下2016-08-08
Android模擬登錄評(píng)論CSDN實(shí)現(xiàn)代碼
本篇文章主要介紹了Android模擬登錄評(píng)論CSDN實(shí)現(xiàn)代碼,可以實(shí)現(xiàn)登陸發(fā)表評(píng)論到官方網(wǎng)站,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
AndroidStudio不自動(dòng)添加新創(chuàng)建的文件到VCS的解決辦法
這篇文章主要介紹了AndroidStudio不自動(dòng)添加新創(chuàng)建的文件到VCS的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03

