在Android WebView中處理文件上傳的步驟如下:
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;
}
});
@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;
}
}
}
<input type="file">
標(biāo)簽或JavaScript來(lái)觸發(fā)文件選擇對(duì)話框。<input type="file" id="fileInput" name="fileInput" />
<script>
var fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', function() {
// Handle file selection
});
</script>
通過(guò)以上步驟,您可以在Android WebView中處理文件上傳操作。您可以根據(jù)需要修改代碼以適應(yīng)您的具體要求。