我正在使用 webview 运行我的网络应用程序,在我的应用程序中,我允许用户将图像上传到我的服务器,在 android > 3 上一切正常,在 Android 2.3.3 浏览器上它也可以按预期工作,但在webview 该文件未填充,并且我的服务器正在获取一个空对象。只有当我从相机拍照时才会发生这种情况(即常规画廊工作正常)。我注意到 Uri 与 file:/// 和 content:/// 不同
附件是我的代码:HTML(我的简单测试页面):
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form id="fuForm" name="fuForm" action="/questions/upload" method="POST" enctype="multipart/form-data">
<label for="fileToUpload">Select a File to Upload</label><br />
<input type="file" name="fileToUpload" id="fileToUpload"/>
<input type="submit"/>
</form>
</body>
</html>
主要活动代码:
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSavePassword(false);
webView.getSettings().setSaveFormData(false);
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new MyWebChromeViewClient());
webView.loadUrl(url);
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
private class MyWebChromeViewClient extends WebChromeClient {
//@Override
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType ) {
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
// Create the storage directory if it does not exist
if (! imageStorageDir.exists()){
imageStorageDir.mkdirs();
}
File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
imageUri = Uri.fromFile(file);
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
cameraIntents.add(intent);
}
uploadMessage = uploadMsg;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
Intent chooserIntent = Intent.createChooser(intent,"Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
activity.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
// For Android < 3.0
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooser(uploadMsg, "");
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
// super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK && requestCode == FILECHOOSER_RESULTCODE) {
if (null == uploadMessage)
return;
final boolean isCamera;
if (intent == null) {
isCamera = true;
} else {
final String action = intent.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action
.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri result;
if (isCamera) {
result = imageUri;
} else {
result = intent == null ? null : intent.getData();
}
uploadMessage.onReceiveValue(result);
} else {
uploadMessage.onReceiveValue(null);
}
uploadMessage = null;
}
任何帮助将不胜感激...
乔。