title: Open and read text file in c++ with Android (ndk)
body: <p>I'm trying to make an app in Android that uses OpenCV in C++. The problem I face is that I want to open a text file from the native part to read it word by word, but it won't open. My code is the following:</p>
  
  <p><strong>JNI method:</strong></p>
  
  <pre><code>JNIEXPORT jboolean JNICALL Java_com_alejandro_nativeopencv_NativeClass_initRecognizer(JNIEnv * env, jclass clazz){
  
      recognizer = Recognizer("ORB","ORB","BruteForce-Hamming");
      recognizer.createObject("/storage/emulated/0/TFG/Fotos/Carpeta", true);
      recognizer.createObject("/storage/emulated/0/TFG/Fotos/Cereales", true);
  
      return true;
  }
  </code></pre>
  
  <p>And the Recognizer::createObject method that I am using:</p>
  
  <p><strong>Recognizer.cpp</strong>:</p>
  
  <pre><code>#include "headers/Recognizer.h"
  #include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;android/log.h&gt;
  
  using namespace std;
  using namespace cv;
  
  //...other methods, constructors and stuff...
  
  Object Recognizer::createObject(String path, bool add) {
      __android_log_print(ANDROID_LOG_DEBUG,"path","%s",(path + "/info.txt").c_str());
      ifstream file((path + "/info.txt").c_str());
      if (!file.is_open()){
          //always enters here
          return Object(true);  //null object
      } else{
          //never enters here
          String name;
          vector &lt; vector&lt;KeyPoint&gt; &gt; keypoints;
          vector &lt;Mat&gt; descriptors;
          vector &lt; vector&lt;Point2f&gt; &gt; corners;
          vector &lt;String&gt; viewNames;
          bool easy;
  
          string word;
          int i = 0;
          while (file &gt;&gt; word)
          {
              if(i == 0){
  
                  /* Object name */
                  name = word;
  
                  __android_log_print(ANDROID_LOG_DEBUG,"NOMBRE","%s",name.c_str());
  
              } else if(i == 1){
  
                  /* Object easy or not */
                  easy = (word == "true");
                  __android_log_print(ANDROID_LOG_DEBUG, "EASY", "%d", easy);
              } else if(i%2 == 0){
  
                  /* Object image view*/
                  keypoints.push_back(vector &lt;KeyPoint&gt;());
                  descriptors.push_back(Mat());
                  corners.push_back(vector &lt;Point2f&gt;(4));
  
                  Mat image = imread(path + "/" + word, CV_LOAD_IMAGE_GRAYSCALE);
                  this-&gt;detector-&gt;detect(image, keypoints[(i/2)-1]);
                  this-&gt;extractor-&gt;compute(image, keypoints[(i/2)-1], descriptors[(i/2)-1]);
                  corners[(i/2)-1][0] = cvPoint(0,0);
                  corners[(i/2)-1][1] = cvPoint(image.cols,0);
                  corners[(i/2)-1][2] = cvPoint(image.cols, image.rows);
                  corners[(i/2)-1][3] = cvPoint(0, image.rows);
                  __android_log_print(ANDROID_LOG_DEBUG, "VISTA", "%d", (i/2)-1);
              } else{
  
                  /* Object name view */
                  viewNames.push_back(word);
  
                  String aux = word;
                  __android_log_print(ANDROID_LOG_DEBUG, "VISTA NOMBRE", "%s", aux.c_str());
              }
  
              i++;
          }
          Object obj = Object(name, keypoints, descriptors, corners, viewNames, easy);
  
          if(add){
              this-&gt;objects.push_back(obj);
          }
  
          file.close();
  
          return obj;
      }
  }
  </code></pre>
  
  <p>The problem is here:</p>
  
  <pre><code>    __android_log_print(ANDROID_LOG_DEBUG,"path","%s",(path + "/info.txt").c_str());
      ifstream file((path + "/info.txt").c_str());
      if (!file.is_open()){
          //always enters here
          return Object(true);  //null object
      } else{
          //never enters here
          ...do stuff...
      }
  </code></pre>
  
  <p>In the JNI method, I pass as parameter the path where the info.txt file that I want to read is located, but it doesn't open it (no error, it just enters in the first if statement. I first tried putting the folder in the sdcard of my mobile phone, and after that in my internal storage, but it doesn't work in any way. I checked in my phone, and I guess the path is correct:</p>
  
  <p><a href="https://s17.postimg.org/gh86vziv3/pantallazo.png" rel="nofollow">Link to the image showing the folder path</a></p>
  
  <p>Also I have in the <strong>Android Manifest</strong> the read and write external storage permission.</p>
  
  <p>Do you know where or what is the problem here?</p>
  
  <p>Thank you very much!</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>Changed code to this:</p>
  
  <pre><code>__android_log_print(ANDROID_LOG_DEBUG,"path","%s",(path + "/info.txt").c_str());
  ifstream file;
  file.open((path + "/info.txt").c_str());
  if (!file.is_open()){
      //always enters here
      return Object(true);  //null object
  } else{
      //never enters here
      ...do stuff...
  }
  </code></pre>
  
  <p>Still having the same problem.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>The log results I'm getting are these (they correspond with the paths it's trying to access):</p>
  
  <pre><code>D/path: /storage/emulated/0/TFG/Fotos/Carpeta/info.txt
  D/path: /storage/emulated/0/TFG/Fotos/Cereales/info.txt
  </code></pre>
  
  <p>The file info.txt is in that path according to my device:</p>
  
  <p><a href="https://s12.postimg.org/fuibza9hp/Screenshot_20160929_132353.png" rel="nofollow">Photo</a></p>