Android Webview中OpenFileChooser的结果导致应用无响应(Result from OpenFileChooser in Android Webview cause the a

系统教程 行业动态 更新时间:2024-06-14 17:01:31
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) { } }

它使用Camera and Gallery选项成功打开'FileChooser'窗口。 如果我选择图像,那么应用程序将完美运行。 问题是,当我回到应用程序而没有选择任何文件时(通过回复或点击应用程序上的屏幕)然后应用程序被卡住(无法点击webview中的任何链接)。

出于测试目的(webview Activity是否处于活动状态)我试图在RESULT_CANCELED条件下调用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_CANCELED情况,并解决这个问题。 提前致谢。

I am developing the hybrid application in android. In this application I am trying to get the image (either from camera or gallery) as user profile picture. Here is the code that i use to get the image.

@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) { } }

It successfully open the 'FileChooser' window with Camera and Gallery option. If I choose the image then the application works perfectly. The problem is, when i coming back to app without choosing any file (via back press or click the screen on app) then the application is stuck (Not able to click any of the link in webview).

For test purpose (Whether webview Activity is in active state) i tried to call the JS function in RESULT_CANCELED condition. Here is my test code

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 }

This function is also not called. (but function is called when the resultCode is RESULT_OK)

Test Device OS version: Android JB 4.3

I stuck in this situation. Can you guys please help me to handle the RESULT_CANCELED situation and mark this problem solved. Thanks in advance.

最满意答案

当你得到RESULT_CANCELLED时,你仍然需要调用mUploadMessage.onReceiveValue。

我想你可以在这种情况下传递null。

You still need to call mUploadMessage.onReceiveValue when you get RESULT_CANCELLED.

I think you can pass null in this case.

更多推荐

本文发布于:2023-04-20 16:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/0392a928866635fbf2f2dede443aa9f9.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:OpenFileChooser   Webview   Android   unrespond   app

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!