我试图在Android 4.4.1&上的webview上打开文件或图像4.4.2。由于为这两个版本删除了回调方法,我向button type="file"
添加了一个事件并显示了一个filechoser。我的问题是在选择文件后,我无法将其发送到button type="file"
。当我尝试更改button type="text"
时,它会显示文件路径名。但我想使用按钮type="file"
。
我的onActivityResult()
代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intentData) {
super.onActivityResult(requestCode, resultCode, intentData);
if (kitkat()) {
String realPathFromURI = "";
String fullPathUri = null;
if (intentData != null) {
Uri uri = intentData.getData();
if (uri != null) {
Uri uri1 = Uri.parse(uri.toString());
realPathFromURI = Utility.getRealPathFromURI(this, uri1);
if (null != realPathFromURI) {
File file = new File(Utility.getRealPathFromURI(this, uri1));
boolean isMaxSize = Utility.isMaxFileSize(file.length());
if (isMaxSize) {
if (this != null)
Toast.makeText(this.getApplicationContext(), "File Size too large",
Toast.LENGTH_SHORT).show();
return;
}
fullPathUri = file.toString();
} else {
if (this != null) {
Toast.makeText(this.getApplicationContext(), "Invalid File Format", Toast.LENGTH_SHORT).show();
}
}
}
} else {
realPathFromURI = "";
fullPathUri = mCameraPhotoPath;
}
String msgToSend = realPathFromURI;
browser.loadUrl("javascript:KitKatWebviewChooseImgResult(\"" + msgToSend + "\")");
} else {
// code for all versions except of Lollipop
}
}
private boolean kitkat() {
if (android.os.Build.VERSION.RELEASE.startsWith("4.4.1") || android.os.Build.VERSION.RELEASE.startsWith("4.4.2")) {
return true;
} else {
return false;
}
}
和HTML代码:
<input name="file" id="files" style="width:195px;font-size:12px;" type="file" data-validation="required" data-validation-error-msg="Please select a file" onclick="showFileChoser()">
脚本:
function showFileChoser() {
choser.choseImage();
}
function KitKatWebviewChooseImgResult(msg) {
document.getElementById("files").value = msg;
}
当我更改input type="text"
时,需要输入和路径显示在webview中。但是当设置input type="text
时,相同的代码无效。
选择文件后,如何将其附加到button type="file"
。
在此先感谢。