Android Webview中OpenFileChooser的结果导致应用程序不响应
Result from OpenFileChooser in Android Webview cause the app unrespond
我正在android中开发混合应用程序。在这个应用程序中,我试图获得图像(从相机或画廊)作为用户配置文件图片。这是我用来获取图像的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
try {
WebSettings webSettings = webView.getSettings();
webSettings.setDatabaseEnabled(true);
webSettings.setDatabasePath("/data/data/"
+ getApplicationContext().getPackageName() + "/databases");
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setRenderPriority(RenderPriority.HIGH);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setSupportZoom(false);
webSettings.setBuiltInZoomControls(false);
webSettings.setDisplayZoomControls(false);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(false);
webView.addJavascriptInterface(_jsInterface, "JSInterface");
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url,
String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
openFileChooser(uploadMsg, acceptType);
}
private void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType) {
mUploadMessage = uploadMsg;
try {
File imageStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"AndroidImageFolder");
if (!imageStorageDir.exists()) {
imageStorageDir.mkdirs();
}
File file = new File(imageStorageDir + File.separator
+ "CAPTURE_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg");
mCapturedImageURI = Uri.fromFile(file);
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.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,
"Pick your avatar");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
new Parcelable[] { captureIntent });
startActivityForResult(chooserIntent,
REQUEST_CODE_FILE_CHOOSER);
} catch (Exception e) {
}
}
});
if (savedInstanceState == null) {
webView.loadUrl("file:///android_asset/index.html");
}
} catch (Exception ex) {
Log.d(TAG, ex.getCause().toString());
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
webView.restoreState(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_CANCELED) {
//Intent returnIntent = new Intent();
//setResult(RESULT_CANCELED, returnIntent);
} else if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE_FILE_CHOOSER:
if (null == mUploadMessage)
return;
Uri result = (data == null || resultCode != RESULT_OK) ? mCapturedImageURI
: data.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
if (result != null) {
String imagePath = "";
String[] imgData = { MediaStore.Images.Media.DATA };
@SuppressWarnings("deprecation")
Cursor imgCursor = managedQuery(result, imgData, null,
null, null);
if (imgCursor != null) {
int index = imgCursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
imgCursor.moveToFirst();
imagePath = imgCursor.getString(index);
} else {
imagePath = result.getPath();
}
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
bitmap = ExifUtil.rotateBitmap(imagePath, bitmap);
// Here goes the bitmap to base64 conversion
try {
handler.post(new Runnable() {
public void run() {
webView.loadUrl("javascript:myJSFunction()"); // Here I return the base64 code (after bitmap conversion)
}
});
} catch (Exception Ex) {
}
}
break;
}
}
} catch (Exception ex) {
}
}
它成功地打开了"文件选择器"窗口和"相机和图库"选项。如果我选择图像,那么应用程序就可以完美工作。问题是,当我回到应用程序而没有选择任何文件(通过按下或点击应用程序上的屏幕)时,应用程序就会被卡住(无法点击网络视图中的任何链接)。
出于测试目的(webview Activity是否处于活动状态),我尝试在RESULT_CANCELD条件下调用JS函数。这是我的测试代码
if(resultCode == Activity.RESULT_CANCELED){
try {
handler.post(new Runnable() {
public void run() {
webView.loadUrl("javascript:myJSFunction()");
}
});
} catch (Exception Ex) {
}
}
else if(resultCode == Activity.RESULT_OK){
..... // Rest of the code
}
也不会调用此函数。(但当resultCode为RESULT_OK时调用函数)
测试设备操作系统版本:Android JB 4.3
我陷入了这种境地。你们能帮我处理RESULT_CANCELD的情况并标记这个问题已经解决吗。提前谢谢。
当您获得RESULT_CANCELLED时,您仍然需要调用mUploadMessage.onReceiveValue。
我认为在这种情况下你可以通过null。
相关文章:
- angular js应用程序不工作
- 我的购物应用程序不会将用户想要购买的东西相加,也不会打印总成本
- Angular应用程序不加载初始页面
- Angular.js ajax和应用程序不起作用
- 地理定位应用程序不工作
- Express子应用程序不工作
- 我的angular应用程序不起作用
- 谷歌应用程序-不;t在使用调试运行脚本期间填充电子表格单元格
- RPS游戏应用程序不工作
- AngularJS-ng应用程序不工作,如果不是emplty
- $injector:nomod模块“应用程序”不可用!仅限火狐浏览器
- 脸书应用程序不起作用
- 使用 firebase 获取$add angular 应用程序不是一个功能
- 使用 JavaScript 或 C# 检查客户端计算机上是否安装了 Java 应用程序(不是 JAVA!)
- RubyonRails应用程序不能包含样式表也不能包含js
- Angularjs 应用程序不会使用 ui-router 路由回主页
- 将ngAnimate添加到控制器会使应用程序不显示内容
- 延迟加载角度应用程序不会加载指令
- 为什么我的HTML5 / JS应用程序不能在我的iPhone上运行
- Android Webview中OpenFileChooser的结果导致应用程序不响应