我对 webview 有同样的问题。假设你html
看起来像
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
这是解决我问题的全部内容
package com.example.webviewtest;
public class MainActivity extends Activity {
private WebView wv;
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wv = new WebView(this);
wv.setWebViewClient(new WebViewClient());
wv.setWebChromeClient(new WebChromeClient() {
// The undocumented magic method override
// Eclipse will swear at you if you try to put @Override here
// For Android < 3.0
public void openFileChooser(ValueCallback uploadMsg) {
Log.i("For Android < 3.0", "called");
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
MainActivity.this.startActivityForResult(
Intent.createChooser(i, "File Browser"),
FILECHOOSER_RESULTCODE);
}
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg,
String acceptType) {
Log.i("For Android 3.0+", "called");
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
MainActivity.this.startActivityForResult(
Intent.createChooser(i, "File Browser"),
FILECHOOSER_RESULTCODE);
}
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFileChooser(uploadMsg);
Log.i("For Android Jellybeans", "called");
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
MainActivity.this.startActivityForResult(
Intent.createChooser(i, "File Browser"),
FILECHOOSER_RESULTCODE);
}
});
setContentView(wv);
wv.loadUrl("http://blue.genetechz.com/qadir/");
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
Log.i("onActivityResult", "called");
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage){
Log.i("if", "return called");
return;
}else{
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
Log.i("else", "inner Called");
}
} else {
Log.i("Else", "Called");
// super.onActivityResult(requestCode, resultCode, intent);
// IPlugin callback = this.activityResultCallback;
// if (callback != null) {
// callback.onActivityResult(requestCode, resultCode, intent);
// }
}
}
@Override
public void onBackPressed() {
if (wv.canGoBack() == true) {
wv.goBack();
} else {
MainActivity.this.finish();
}
}
}