如何使上传按钮在android应用程序中工作? [英] How to make upload button work in android app?
问题描述
这个问题被多次遇到,但似乎没有任何解释有效.(或者也许我在这个叫做互联网的混乱中没有找到它)......
This question is encountered several times, though there doesn't seem to be no explanation that works. (or maybe I didn't find it in this chaos called internet)...
我正在开发一个 android 应用程序,它会打开一个包含上传按钮的 HTML 页面.它在 WebView 中不起作用.
I am developing an android app that opens a HTML page that contains an upload button. It doesn't work in WebView.
我试过了:http://m0s-programming.blogspot.in/2011/02/file-upload-in-through-webview-on.html
但是 eclipse 给出警告 openFileChooser(ValueCallback uploadMsg) 永远不会在本地使用"
.该应用应适用于 Android 2.2 (API 8) 及更高版本.
but eclipse gives warning that "openFileChooser(ValueCallback uploadMsg) is never used locally"
. The app should work with Android 2.2 (API 8) and above.
它给出了一些错误,我猜是由于 WebView.setWebChromeClient(new CustomWebChromeClient()
It give some errors, I guess due to wrong placement of WebView.setWebChromeClient(new CustomWebChromeClient()
有人可以帮我解决这个问题吗?
Can someone help me on this?
推荐答案
这里回答了关于文件上传的类似问题:在WebView中上传文件.
A similar question about file upload was answered here: File Upload in WebView.
另外,不同版本的 Android 需要不同的方法:https://stackoverflow.com/posts/12746435/edit
Also different versions of Android require different methods: https://stackoverflow.com/posts/12746435/edit
这是活动的完整且自给自足的代码:
Here is full and self-sufficient code of the activity:
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;
}
}
}
这篇关于如何使上传按钮在android应用程序中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!