Android Webview中OpenFileChooser的结果导致应用无响应

时间:2014-10-16 07:33:59

标签: javascript android-webview webchromeclient

我正在开发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) {
    }
}

使用“相机和图库”选项成功打开“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 情况,并解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:0)

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

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