title: onActivityResult return resultCode = -1
body: <p>I have this code that allows me to select file from the SDcard and upload it to server, it was working fine in an activity. Now i switched to fragment and used the same code but it's not working. What is happening is that the onActivityResult() is called and the window to select file pop-up and when I select a file, nothing happens resultCode = -1. I don't know how to fix this.</p>
  
  <p>Here is the code from the onActivityResult():</p>
  
  <pre><code>public void selectFile() {
  
      Intent intent = new Intent();
      intent.setType("*/*");
      intent.setAction(Intent.ACTION_GET_CONTENT);
      intent.addCategory(Intent.CATEGORY_OPENABLE);
      startActivityForResult(Intent.createChooser(intent, "Select File"), FILE_SELECT_CODE);
  
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      Log.i("Here ","onActivityResult");
      Log.i("Here ","onActivityResult");
      Log.i("resultCode ",resultCode+"");
      if (resultCode == RESULT_OK) {
          Log.i("Here ","onActivityResult");
          if (requestCode == FILE_SELECT_CODE) {
              Log.i("Here ","onActivityResult");
              Uri selectedFileUri = data.getData();
              String uriString = selectedFileUri.toString();
              Log.i("UriString ", uriString);
              File fileToUpload = new File(uriString);
  
  
              if (uriString.startsWith("content://")) {
                  Cursor cursor = null;
                  try {
                      cursor = getActivity().getContentResolver().query(selectedFileUri, null, null, null, null);
                      if (cursor != null &amp;&amp; cursor.moveToFirst()) {
                          uploadFileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                      }
                      uploadFilePath = getPath(selectedFileUri);
                      Log.i("Upload File Path ", uploadFilePath);
                  } finally {
                      cursor.close();
                  }
              } else if (uriString.startsWith("file://")) {
                  uploadFileName = fileToUpload.getName();
                  uploadFilePath = uriString.replace("file://", "");
                  Log.i("Upload File Path ", uploadFilePath);
              }
          }
      }
  }
  
  
  
  
  private String getPath(Uri selectedFileUri) {
      if (selectedFileUri == null)
          return null;
      String[] projection = {MediaStore.Images.Media.DATA};
      Cursor cursor = getActivity().getContentResolver().query(selectedFileUri, projection, null, null, null);
      if (cursor != null) {
          int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
          cursor.moveToFirst();
          return cursor.getString(column_index);
      }
      return selectedFileUri.getPath();
  }
  </code></pre>