我有一个简单的应用程序,可以选择上传图像(画廊或相机),我使用 webview 并设法使其工作。
当我打开“openfilechoosr”对话框并返回我的应用程序而不选择任何文件(通过背面或只是单击我的应用程序屏幕)时,我遇到了问题,而不是我的应用程序不再响应(直到 android 重新启动),当我重新启动它,它显示为一个空白页。
更多信息:
每次我在没有选择任何文件的情况下退出文件选择器后尝试打开应用程序时都会遇到的错误(应用程序将在重新启动 android 后再次运行):
java.lang.Throwable:在设置 WebViewCore 之前不支持 EventHub.removeMessages(int what = 107)
我的代码:[MainActivity]
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);
}
请指教。
谢谢乔。