我有一个带文件选择器的webview,可以在Samsung Galaxy Tab 2(Android 4.1.1), Lenovo, Nexus Tablets
中使用。但问题是它不是在Samsung galaxy Tab 3
中徘徊。哪个是Android 4.4 tablet
。我在这里添加了setWebChromeClient
代码。你能帮助别人吗?
// implement WebChromeClient inner class
// we will define openFileChooser for select file from camera
webView.setWebChromeClient(new WebChromeClient() {
// openFileChooser for Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType) {
/** updated, out of the IF **/
mUploadMessage = uploadMsg;
/** updated, out of the IF **/
Log.e("Reac", "**Here");
try {
File imageStorageDir = new File(base_directory,
"profile_pictures");
if (!imageStorageDir.exists()) {
imageStorageDir.mkdirs();
}
src_file = new File(imageStorageDir + File.separator
+ "IMG_" + child_id + ".jpg");
mCapturedImageURI = Uri.fromFile(src_file); // save to the
// private
// variable
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
mCapturedImageURI);
startActivityForResult(captureIntent,
FILECHOOSER_RESULTCODE);
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Camera Exception:" + e,
Toast.LENGTH_LONG).show();
}
}
// openFileChooser for Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooser(uploadMsg, "");
}
// openFileChooser for other Android versions
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
openFileChooser(uploadMsg, acceptType);
}
/** Added code to clarify chooser. **/
// The webPage has 2 filechoosers and will send a console message
// informing what action to perform, taking a photo or updating the
// file
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", "Per cÔøΩnsola: " + message);
}
/** Added code to clarify chooser. **/
});
我只添加了一段代码。请问我是否需要任何细节。
答案 0 :(得分:4)
Android 4.4中似乎有known bug使<input type="file">
无法在WebView中运行。
不幸的是openFileChooser
不是公共API,也没有公众支持。
在Android中,Lollipop为此目的引入了API onShowFileChooser。
我的建议是使用JavaScript native WebView interface解决问题,并定义由应用的原生部分处理的自定义openFileChooser API。