我有一个本机 android 应用程序来显示一个网站。我是新手。我使用了一个 webview 对象来显示该站点,除了文件上传之外,一切都很完美。我尝试了我在互联网上看到的任何东西,但我找不到解决这个问题的方法。当我点击文件上传时,什么也没有发生。没有错误,没有其他操作......我只是收到一个警告,如“openFileChooser 从未在本地使用”。我阅读了有关此警告的文章,但我真的被卡住了。我正在 Galaxy S3 冰淇淋三明治上测试它。任何帮助将不胜感激。
WebView webView;
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE=1;
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage) return;
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form_content_view);
// Show the Up button in the action bar.
setupActionBar();
Intent intent = getIntent();
final String message = intent.getStringExtra(ListOfForms.EXTRA_MESSAGE);
WebViewClient wvc=new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (errorCode == 401) {
webView.setHttpAuthUsernamePassword("http://project.mysite.com","mydomain","myuser","mypass");
}
}
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
handler.proceed("myuser", "mypass");
}
};
WebChromeClient wcc = new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
FormContentView.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);
}
};
webView = (WebView) findViewById(R.id.wvFormContent);
webView.setWebChromeClient(wcc);
webView.setWebViewClient(wvc);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setGeolocationEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl("http://project.mysite.com");
String myVariable = "dummy";
webView.loadUrl("javascript:document.getElementById('txtMyVariable').text = '"+myVariable+"';");
}
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.form_content_view, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}