我有一个 webview 显示一些网页,该网页包括一个 iframe 让用户选择图片并上传它们。我的问题是当我单击按钮并选择一张照片时,在 onActivityResult 方法之后,它会刷新整个主页而不是 iframe 页面。如何仅刷新 iframe 页面?
主页
<iframe scrolling="no" src="/service/ccrp.do?method=img"></iframe>
<img id="uploadImg" name="uploadImg" src="" style="display: none;"/>
<input type="hidden" id="imgUrl" name="imgUrl" />
iframe 页面(/service/ccrp.do?method=img):
function sub() {
var path = $("#file").val();
var fs = path.split("\\");
var fileName = fs[fs.length - 1];
$("#file_form")[0].submit();
}
<form id="file_form" onsubmit="return false;"
ENCTYPE="multipart/form-data" method="post"
action="/module/bbs/mobile.do?method=upload">
<input id="file" name="file" type="file" onchange="sub()">
</form>
WebviewActivity
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
initWebview();
}
private void initWebview(){
WebSettings settings = webview1.getSettings();
settings.setAllowFileAccess(true);
settings.setJavaScriptEnabled(true);
webview1.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
webview1.setWebChromeClient(new WebChromeClient() {
// For Android 4+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
openFileChooser( uploadMsg );
}
// For Android 3.0+
public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType )
{
openFileChooser( uploadMsg );
}
// For Android < 3.0
public void openFileChooser( ValueCallback<Uri> uploadMsg )
{
WebviewActivity.this.mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
WebviewActivity.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), FILECHOOSER_RESULTCODE );
}
});
webview1.loadUrl(url);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
switch (requestCode) {
// Choose a file from the file picker.
case FILECHOOSER_RESULTCODE:
if (null == mUploadMessage)
break;
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
Log.v("CEEG_RESULT", "onActivityResult:" + result.getQuery());
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
break;
}
}