2017-02-13 104 views
0

在下面的代碼中,當立即使用RESULT_CANCELED調用onActivityResult時。在startActivityForResult與RESULT_CANCELED之後立即調用Cordova onActivityResult

正如其他答案中所建議的,我在startActivityForResult()和PluginResult#setKeepCallback(true);之前添加了setActivityResultCallback。但沒有任何幫助。 有什麼建議嗎?

.... 
    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")); 
    } 
} 
+0

您是否找到解決方案@dpaksoni? – user3050534

回答

0

我面臨着同樣的問題,在我的科爾多瓦應用之一。我找不到錯誤的來源,但我通過創建新項目並重新安裝所有插件來解決它。有效。

+0

我試了一下,它的工作。但仍然困惑以前項目中的問題。 – dpaksoni

0

我encoutered類似的問題,我的應用程序並安裝科爾多瓦 - 插件,後臺模式之後。對我來說,這個問題存在於android 4.4.2上,而不是android 6.0上。爲了解決這個問題,我設置的參數AndroidLaunchMode config.xml中

<preference name="AndroidLaunchMode" value="singleTop" /> 
相關問題