它是一个带有webview的简单Web应用程序。并且有一个农场,有一个按钮可以从相机/相册中选择图像,但是我当前的代码无法正常工作,任何人都可以帮忙。这是我的代码
onCreate 中的
webView = findViewById(R.id.webView);
webView.setScrollbarFadingEnabled(true);
webView.setHorizontalScrollBarEnabled(false);
webView.getSettings().setUserAgentString("AndroidWebView");
webView.setWebViewClient(new MyWebClient());
webView.setWebViewClient(new WebViewClient());
webView.clearCache(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setAllowFileAccess(true);
webView.setBackgroundColor(Color.parseColor("#000000"));
webView.setBackgroundColor(Color.TRANSPARENT);
webView.loadUrl("URL");
然后在这之后是我的 setWebChromeClient
webView.setWebChromeClient(new WebChromeClient() {
// openFileChooser for Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType) {
mUploadMessage = uploadMsg;
try {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File externalDataDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File cameraDataDir = new File(externalDataDir.getAbsolutePath() + File.separator + "browser-photos");
cameraDataDir.mkdirs();
String mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator + System.currentTimeMillis() + ".jpg";
mCapturedImageURI = Uri.fromFile(new File(mCameraFilePath));
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
mCapturedImageURI);
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
new Parcelable[] { cameraIntent });
startActivityForResult(chooserIntent,
FILECHOOSER_RESULTCODE);
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Camera Exception:" + e,
Toast.LENGTH_LONG).show();
}
}
// For Android < 3.0
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooser(uploadMsg, "");
}
// For Android > 4.1.1
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFileChooser(uploadMsg, acceptType);
}
public boolean onConsoleMessage(ConsoleMessage cm) {
onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId());
return true;
}
public void onConsoleMessage(String message, int lineNumber,
String sourceID) {
Log.d("androidruntime", "www.example.com: " + message);
}
});
和在onCreate外面
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
// TODO Auto-generated method stub
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == this.mUploadMessage) {
return;
}
Uri result = null;
try {
if (resultCode != RESULT_OK) {
result = null;
} else {
result = intent == null ? mCapturedImageURI : intent
.getData();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "activity :" + e,
Toast.LENGTH_LONG).show();
}
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
在我的其他应用程序中,此代码工作正常,但在这一个中它不起作用。当我从webview按下浏览按钮时,它根本不做任何事情。
答案 0 :(得分:0)
在api21之上,该方法应该是onShowFileChooser。如果您的设备高于LOLLIPOP,请确保您的代码无法正常工作。
在这里查看文档: onShowFileChooser