在带有RESULT_CANCELED的startActivityForResult之后立即调用Cordova onActivityResult [英] Cordova onActivityResult is called immediately after startActivityForResult with RESULT_CANCELED

查看:225
本文介绍了在带有RESULT_CANCELED的startActivityForResult之后立即调用Cordova onActivityResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,当使用RESULT_CANCELED立即调用onActivityResult时.

In the code below, when onActivityResult is called immediately with RESULT_CANCELED.

如其他答案中所建议,我在startActivityForResult()和PluginResult#setKeepCallback(true);之前添加了setActivityResultCallback.但是没有任何帮助. 有什么建议吗?

As suggested in other answers, I have added setActivityResultCallback just before startActivityForResult() and PluginResult#setKeepCallback(true);. But nothing is helping out. Any suggestion?

 ....
  public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {

    this.callbackContext  = callbackContext;

    if (action.equals(ACTION_OPEN))
    {
        if(PermissionHelper.hasPermission(this, READ))
        {
            chooseFile();
        }
     }
    else
    {
        return false;
    }

    return true;
}

  public void chooseFile() {

    final CordovaPlugin plugin = (CordovaPlugin) this;
    Runnable worker = new Runnable() {
        public void run() {
            Intent filePickerIntent = new Intent(Intent.ACTION_PICK);
            filePickerIntent.setType("image/*");
            plugin.cordova.setActivityResultCallback(plugin);
            plugin.cordova.startActivityForResult(plugin, Intent.createChooser(filePickerIntent,"Choose file"), PICK_FILE_REQUEST);
        }
    };
    this.cordova.getThreadPool().execute(worker);
    PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
    r.setKeepCallback(true);
    callbackContext.sendPluginResult(r);

}

 public void onActivityResult(int requestCode, int resultCode, Intent data) {

    Log.d(TAG,"Enter onActivityResult");

    if (requestCode == PICK_FILE_REQUEST) {

        Log.d(TAG,"requestCode == PICK_FILE_REQUEST");

        if (resultCode == Activity.RESULT_OK) {

            Log.d(TAG,"Result Ok");

            Uri uri = data.getData();
            Log.d(TAG, uri.toString());

        } else if (resultCode == Activity.RESULT_CANCELED) {

            Log.d(TAG,"Result canceled");

            callbackContext.error("OPERATION_CANCELLED");
            return;
        }

        this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "UNKNOWN_ERROR"));
    }
}

推荐答案

我在我的cordova应用程序之一中遇到了同样的问题.我找不到错误源,但我通过创建新项目并再次安装所有插件来解决了该错误.奏效了.

I faced the same issue in one of my cordova application. I could not find the source of error but I resolved it by creating new project and installing all plugins again. It worked.

这篇关于在带有RESULT_CANCELED的startActivityForResult之后立即调用Cordova onActivityResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆