有關文件上傳的類似問題在這裏回答:File Upload in WebView。
而且不同版本的Android需要不同的方法:https://stackoverflow.com/posts/12746435/edit
下面是活動的完全自足代碼:
public class FileAttachmentActivity extends Activity {
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView 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
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
FileAttachmentActivity.this.showAttachmentDialog(uploadMsg);
}
// For Android > 3.x
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
FileAttachmentActivity.this.showAttachmentDialog(uploadMsg);
}
// For Android > 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
FileAttachmentActivity.this.showAttachmentDialog(uploadMsg);
}
});
this.setContentView(wv);
wv.loadUrl("https://dl.dropbox.com/u/8047386/posttest.htm");
}
private void showAttachmentDialog(ValueCallback<Uri> uploadMsg) {
this.mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
this.startActivityForResult(Intent.createChooser(i, "Choose type of attachment"), FILECHOOSER_RESULTCODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == this.mUploadMessage) {
return;
}
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
this.mUploadMessage.onReceiveValue(result);
this.mUploadMessage = null;
}
}
}
仔細閱讀文章:'4)關於openFileChooser從未在本地使用的警告是正常的,事實上我很確定它應該給你警告。如果它不適合你,這個問題就在別的地方。「# – vorrtex 2013-03-16 10:26:01
@vorrtex它確實給了我警告。但即使忽視這一點,該應用程序實際上在我的模擬器上崩潰。 – Chirag 2013-03-16 10:36:14
我在文章的評論中發現了一個示例應用程序,經過一些小修改後,它就可以工作。 https://dl.dropbox.com/u/8047386/file-attach-cordova-upload-jqm-master_fixed.zip。我相信你的應用程序中有不同的東西可以壓制,而上傳工作正常。 – vorrtex 2013-03-16 11:06:35