Android WebView鏂囦欢涓婁紶濡備綍澶勭悊
鍦ˋndroid WebView涓鐞嗘枃浠朵笂浼犵殑姝ラ濡備笅锛?/p>
- 璁剧疆WebChromeClient瀵硅薄锛氳澶勭悊鏂囦欢涓婁紶锛屽繀椤讳负WebView璁剧疆涓€涓猈ebChromeClient瀵硅薄骞堕噸鍐檕nShowFileChooser鏂规硶銆傚湪璇ユ柟娉曚腑锛屽彲浠ュ垱寤轰竴涓狥ileChooser瀵硅薄骞舵樉绀烘枃浠堕€夋嫨鍣ㄣ€?/li>
webView.setWebChromeClient(new WebChromeClient() {
// For Android 4.1+
public 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 Chooser"), FILE_CHOOSER_RESULT_CODE);
}
// For Android 5.0+
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
mFilePathCallback = filePathCallback;
Intent intent = fileChooserParams.createIntent();
startActivityForResult(intent, FILE_CHOOSER_RESULT_CODE);
return true;
}
});
- 澶勭悊鏂囦欢閫夋嫨缁撴灉锛氬湪Activity鐨刼nActivityResult鏂规硶涓鐞嗘枃浠堕€夋嫨缁撴灉锛屽苟灏嗛€夊畾鐨勬枃浠朵紶閫掔粰WebView銆?/li>
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FILE_CHOOSER_RESULT_CODE) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
if (mUploadMessage == null)
return;
Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
} else {
if (mFilePathCallback == null)
return;
mFilePathCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
mFilePathCallback = null;
}
}
}
- 鍦╓eb椤甸潰涓皟鐢ㄦ枃浠朵笂浼狅細鍦╓eb椤甸潰涓皟鐢ㄦ枃浠朵笂浼犳椂锛屽彲浠ヤ娇鐢?code><input type="file">鏍囩鎴朖avaScript鏉ヨЕ鍙戞枃浠堕€夋嫨瀵硅瘽妗嗐€?/li>
<input type="file" id="fileInput" name="fileInput" />
<script>
var fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', function() {
// Handle file selection
});
</script>
閫氳繃浠ヤ笂姝ラ锛屾偍鍙互鍦ˋndroid WebView涓鐞嗘枃浠朵笂浼犳搷浣溿€傛偍鍙互鏍规嵁闇€瑕佷慨鏀逛唬鐮佷互閫傚簲鎮ㄧ殑鍏蜂綋瑕佹眰銆?/p>
相关问答